I'm trying to figure out qsort(). I haven't seen any practical examples,
only synopsis. In the code below, the array is not sorted. Can someone
give me some help?
#include <stdio.h>
#include <stdlib.h>
int compare(const void* a, const void* b);
int main(void)
{
int idx;
int array[] = {243, 12, 99, 106, 122, 77, 242};
qsort(array, 7, 4, &compare);
for(idx=0; idx<7; ++idx)
printf("%d\t", array[idx]);
printf("\n");
return 0;
}
int compare(const void* a, const void* b)
{
if(a < b) return -1;
if(a == b) return 0;
if(a > b) return 1;
}
only synopsis. In the code below, the array is not sorted. Can someone
give me some help?
#include <stdio.h>
#include <stdlib.h>
int compare(const void* a, const void* b);
int main(void)
{
int idx;
int array[] = {243, 12, 99, 106, 122, 77, 242};
qsort(array, 7, 4, &compare);
for(idx=0; idx<7; ++idx)
printf("%d\t", array[idx]);
printf("\n");
return 0;
}
int compare(const void* a, const void* b)
{
if(a < b) return -1;
if(a == b) return 0;
if(a > b) return 1;
}
Comment