qsort: sorts the array *buf using a Quicksort
//Declaration: void qsort(void *buf, size_t num, size_t size, int (*compare) (const void *, const void *));
#include <stdlib.h>
#include <stdio.h>
int num[10] = {
1, 3, 6, 5, 8, 7, 9, 6, 2, 0
};
int comp(const void *, const void *);
int main(void)
{
int i;
printf("Original array: ");
for(i=0; i<10; i++) printf("%d ", num[i]);
qsort(num, 10, sizeof(int), comp);
printf("Sorted array: ");
for(i=0; i<10; i++) printf("%d ", num[i]);
return 0;
}
/* compare the integers */
int comp(const void *i, const void *j)
{
return *(int *)i - *(int *)j;
}
/*
Original array: 1 3 6 5 8 7 9 6 2 0 Sorted array: 0 1 2 3 5 6 6 7 8 9 */
Related examples in the same category