#include <stdio.h>
#include <stdlib.h>
float values[] = { 4, 1, 10, 9, 2, 5, -1, -9, -2,10000,-0.05,-3,-1.1 };
int compare (const void * a, const void * b)
{
return ( (int) (*(float*)a - *(float*)b) );
}
int main ()
{
int i;
qsort (values, 13, sizeof(float), compare);
for (i = 0; i < 13; i++)
{
printf ("%f ",values[ i ]);
}
putchar('\n');
return 0;
}
The result is:
-9.000000 -3.000000 -2.000000 -1.000000 -1.100000 -0.050000 1.000000 2.000000 4.000000 5.000000 9.000000 10.000000 10000.000000
It's wrong because the order of -1 and -1.1 is changed. I believe it is happening because my "compare" function.
How can I fix this?
Thanks