void qsort(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *))qsort sorts an array of nelem objects,
each size bytes long and the first of which is pointed
to by base, using the comparison function pointed to
by compar.
compar should return:
qsort is not a stable sort, meaning that "equal" items
can change order across sorts.
qsort Example
#include <stdlib.h>
int ilist[] = { 17, 5, 31, 32, 7, 5, 1 };
size_t ilsiz = sizeof(ilist)/sizeof(ilist[0]);
const char *slist[] = {
"abc",
"def",
"alpha",
"bet",
"alfa",
"romeo"
};
size_t slsiz = sizeof(slist)/sizeof(slist[0]);
int
lo2hi(const void *vp0, const void *vp1)
{
const int *ip0 = (const int *)vp0;
const int *ip1 = (const int *)vp1;
printf("lo2hi: Compare %d and %d returns %d\n", *ip0, *ip1, *ip0 - *ip1);
return(*ip0 - *ip1);
}
int
hi2lo(const void *vp0, const void *vp1)
{
const int *ip0 = (const int *)vp0;
const int *ip1 = (const int *)vp1;
return(*ip1 - *ip0);
}
int
firstchar(const void *vp0, const void *vp1)
{
const char **cpp0 = (const char **)vp0;
const char **cpp1 = (const char **)vp1;
return(**cpp0 - **cpp1);
}
int
main(void)
{
int i;
qsort(ilist, ilsiz, sizeof(ilist[0]), lo2hi);
printf("\nSorted Low to High\n-----------\n");
for (i = 0; i < ilsiz; i++) {
printf("%d: %d\n", i, ilist[i]);
}
qsort(ilist, ilsiz, sizeof(ilist[0]), hi2lo);
printf("\nSorted High to Low\n-----------\n");
for (i = 0; i < ilsiz; i++) {
printf("%d: %d\n", i, ilist[i]);
}
qsort(slist, slsiz, sizeof(slist[0]), firstchar);
printf("\nSorted by First Character\n-----------\n");
for (i = 0; i < slsiz; i++) {
printf("%d: %s\n", i, slist[i]);
}
return(0);
}
lo2hi: Compare 17 and 32 returns -15 lo2hi: Compare 5 and 32 returns -27 lo2hi: Compare 31 and 32 returns -1 lo2hi: Compare 32 and 1 returns 31 lo2hi: Compare 32 and 7 returns 25 lo2hi: Compare 32 and 5 returns 27 lo2hi: Compare 17 and 1 returns 16 lo2hi: Compare 1 and 5 returns -4 lo2hi: Compare 1 and 7 returns -6 lo2hi: Compare 31 and 1 returns 30 lo2hi: Compare 5 and 1 returns 4 lo2hi: Compare 5 and 17 returns -12 lo2hi: Compare 31 and 17 returns 14 lo2hi: Compare 17 and 5 returns 12 lo2hi: Compare 17 and 7 returns 10 lo2hi: Compare 5 and 5 returns 0 lo2hi: Compare 5 and 7 returns -2 Sorted Low to High ----------- 0: 1 1: 5 2: 5 3: 7 4: 17 5: 31 6: 32 Sorted High to Low ----------- 0: 32 1: 31 2: 17 3: 7 4: 5 5: 5 6: 1 Sorted by First Character ----------- 0: abc 1: alfa 2: alpha 3: bet 4: def 5: romeo
void *bsearch(const void *key, const void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *))bsearch searches an array of nelem objects,
each size bytes long and the first of which is pointed
to by base, for an element that matches the object
pointed to by key using the comparison function pointed to
by compar.
compar takes two arguments, a pointer to key
and a pointer to an array element, and returns:
key is found to be less than the array element
key is equal to the array element
key is found to be greater than the array element
bsearch returns a pointer to the first matching element, or
a null pointer if no matching elements were found.
key,
the element matched is unspecified.