Code Snippets C

//Declaration: void qsort(void *buf, size_t num, size_t size, int (*compare) (const void *, const void *));
#include
#include
int num[10] = {
1, 4, 6, 5, 8, 9, 7, 6, 2, 0
};
int comp(const void *, const void *);
int main(void)
{
int x;
printf("Original array: ");
for(x=0; x<10; x++) printf("%d ", num[x]);
qsort(num, 10, sizeof(int), comp);
printf("Sorted array: ");
for(x=0; x<10; x++) printf("%d ", num[x]);
return 0;
}
/* compare the integers */
int comp(const void *x, const void *j)
{
return *(int *)x - *(int *)j;
}
/*
Original array: 1 4 6 5 8 9 7 6 2 0 Sorted array: 0 1 2 4 5 6 6 7 8 9 */