Hi,
I am sorting an double array, using qsort()

#include<stdio.h>
#include<stdlib.h>
...
int compar( const void *arg1, const void *arg2 )
{
return ( * ( double **) arg1 >= * (double** ) arg2 );
}
....
int main(int argc,char *argv[]){
...
qsort(array, size_ct,sizeof(double),compar);
...
}

It turns out that the following array:
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.727436
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.727436
0.000000
0.000000
0.000000
0.000000
0.000000
0.727436
0.000000
0.000000
0.727436
0.000000
0.727436
0.727436
0.000000
0.000000
0.000000
0.000000
0.727436
0.000000
0.000000

is sorted as :
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.727436
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.727436
0.727436
0.727436
0.727436
0.727436
0.727436

This is not correct with a 0.727436 placed between 0.000000s. Can you point out why it is wrong? And is qsort() in C standard library and available in both Windows and Linux?
Thanks!

Of course, it's incorrect result because your compar is wrong.
Read qsort specification carefully. That's a quote from the C language standard:

The contents of the array are sorted into ascending order according to a comparison function pointed to by compar, which is called with two arguments that point to the objects being compared. The function shall return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

The compar must return three possible values!
Another error: extract comparable values with that expression:

*(double*)arg

I hope you can correct the code yourself ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.