The following is my code:
#include <stdlib.h>
#include <stdio.h>
#include <limits>
#define INFINITY std::numeric_limits<float>::infinity()
#define NEGINFINITY -std::numeric_limits<float>::infinity()
int floatcomp(const void* elem1, const void* elem2)
{
if (*(const float*)elem1 < *(const float*)elem2)
return -1;
return *(const float*)elem1 > *(const float*)elem2;
}
int main()
{
float array[10] = {INFINITY, 3.5f, 144.4f, NAN, 12.4f, NEGINFINITY, 1.4f, -0.0f, 5.9f};
int i;
for (i = 0; i < 10; i++)
printf("%f\n", array[i]);
printf("\n");
qsort(array, 10, sizeof(float), floatcomp);
for (i = 0; i < 10; i++)
printf("%f\n", array[i]);
return 0;
}
The quicksort algorithm sorts the numbers entered in the correct order however, there is a 0.00000 always present in the list.
Also while adding the NaN to the array, the NaN is not sorted properly as it should be and I'm not able to correctly add in two different mantissa's for the NaN into my code as a string at the top.