|
|
#include <stdlib.h>void qsort (void *base, size_t nel, size_t width, int (*compar) (const void *, const void *));
The arguments are:
The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.
The order in the output of two items which compare as equal is unpredictable.
/* Comparison rtn for qsort on array of ints - note casting */
int
intcmp(const void *aa, const void *bb)
{
int *a = (int *) aa;
int *b = (int *) bb;
return(*a - *b); /* ascending order */
/* return(*b - *a); /* descending order */
}
...
int list[SIZE];
size_t nelems = SIZE; /* or sizeof(list)/sizeof(int) */
...
qsort((void*)list, nelems, sizeof(int), intcmp);
X/Open Portability Guide, Issue 3, 1989
;
ANSI X3.159-1989 Programming Language -- C
;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1)
;
and
NIST FIPS 151-1
.