I am trying to sort an array in descending order with the help of qsort in C.
#include<stdio.h>
#include<stdlib.h>
int compareFloat(const void* p1, const void* p2){
const float* pa = p1;
const float* pb = p2;
return pb - pa;
}
int main()
{
float arr[] = {1.5, 1.6, 1.38};
qsort(arr, 3, sizeof(float), compareFloat);
printf("%.2f %.2f %.2f", arr[0], arr[1], arr[2]);
return 0;
}
The output for above code is "1.60 1.38 1.50", which is wrong; but, when the array is initialized as float arr[] = {1.38, 1.6, 1.5}, the answer is correct (i.e. "1.6, 1.5, 1.38").
Why?