Tomáš Čičman
Tomáš Čičman

Reputation: 281

How correctly sort an array?

I have this code for sort array polsum:

int comp (const void * a, const void * b){   
   double aa = *(double*)a, bb = *(double*)b;

   if (aa < bb) return -1;
   if (aa > bb) return  1;
   return 0;
}

double sort(double *polsum){                              
    int p;
    qsort(polsum, sizeof(double),sizeof(double), comp);
    return 0;
}

But output is:

5,01/
80,86/
85,01/
85,01/
300,88/
600,88/
77888,88/
100400,00/
670,88/
80,86/
80,86/

Where have I made a mistake?

Upvotes: 1

Views: 57

Answers (1)

jeremy
jeremy

Reputation: 4314

Your passing in sizeof(double) for the count of elements to be sorted. sizeof(double) == 8

Consider adding the following to your sort function:

double sort(double *polsum, int count) {
    int p;
    qsort(polsum, count, sizeof(double), comp);
    return 0;
}

Upvotes: 1

Related Questions