Function prototypes
#include <stdlib.h>
void Qsort (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));
Parameter description:
Base: The starting address of the sorted array
NMEMB: The number of elements to sort
Size: A single element
Compar: User-defined method for comparing two element sizes.
Int (*compar) (const void *P1, const void *P2) function description
Ascending: Returns a positive number when P1 is greater than P2, returns 0 for equality, less than return negative
Descending: Returns a negative number when P1 is greater than P2, returns 0 for equality, less than the return positive number
eg
#include <stdio.h> #include <stdlib.h> #include <string.h>static intcmp (const void *P1, const void *P2) {return * (int*) P1-* (int*) P2;} Intmain (int argc, char *argv[]) {int j;int s[10]={1,3,5,7,9,2,4,6,8,0};qsort (&s[0], ten, sizeof (S[0]), CMP); for (j = 0; J < 10; J + +) printf ("%d\n", S[j]); exit (exit_success);}
Run results
The use of Qsort in Linux