Zookeeper
Bubble and fast sorting are common sorting methods. Here we use the qsort function in the header file for sorting. However, you must first create a CMP function.
# Include <stdlib. h> // qsort header file int A [100] = }; // The array struct person to be sorted // struct {char num [20]; char name [100]; int score; int sum;} Man [100];
1. Sort array []
Int cmp_1 (const void * a, const void * B) // sort the array a [] {return * (int *) A-* (int *) B ;} // call the function qsort (A, N, sizeof (A [0]), cmp_1); // n is the size of the array, where 9
2. sort by the score in the struct
Int cmp_2 (const void * a, const void * B) // rank {return (person *) A)-> score-(person *) by the score in the structure *) b)-> score;} // function call qsort (man, N, sizeof (MAN [0]), cmp_2 );
3. sort by sum first. If sum is equal, then sort by score.
Int cmp_3 (const void * a, const void * B) // first sort by sum. If sum is equal, then rank by score {struct person * c = (person *); struct person * D = (person *) B; If (c-> sum! = D-> sum) return C-> sum-D-> sum; else return C-> score-D-> score ;}
// Function call qsort (man, N, sizeof (MAN [0]), cmp_3 );
4. First sort by string num. If the string num is the same, then sort by string name.
Int cmp_4 (const void * a, const void * B) // first sort by string num. If the string num is the same, then rank {struct person * c = (person *) A by string name; struct person * D = (person *) B; If (strcmp (c-> num, d-> num )! = 0) return strcmp (c-> num, D-> num); else strcmp (c-> name, D-> name );}
// Function call qsort (man, N, sizeof (MAN [0]), cmp_4 );
5. sort by score first. If the score is the same, then sort by string num.
<PRE class = "CPP" name = "code"> int cmp_5 (const void * a, const void * B) // sort by score first. If the score is the same, {struct person * c = (person *) A; struct person * D = (person *) B; If (c-> score! = D-> score) return C-> score-D-> score; else return strcmp (c-> num, D-> num );} // function call qsort (man, N, sizeof (MAN [0]), cmp_5 );