Home > Blockchain >  Qsort sort from small to high
Qsort sort from small to high

Time:10-14

does somebody know how to write the comparefunction of qsort? I want the comparefunction1 to sort the array from highest to lowest and comparefunction2 to sort another array from lowest to highest. My Code:

   int cmpfunc(const void *a, const void *b) {                                                                                             
       if (*(double*) a < *(double*) b)
           return 1;
       else if (*(double*) a > *(double*) b)
           return -1;
       else
           return 0;
   }

   int cmpfunc2(const void *a, const void *b) {
       if (*(float*) a < *(float*) b)
           return -1;
       else if (*(float*) a > *(float*) b)
           return 1;
       else
           return 0;
   }

sadly both compare functions sort from highest to lowest. I want one to sort from lowest to highest. The qsort looks like this:

#include "stdio.h"
int main(){
qsort(Acal, 4, sizeof(float), cmpfunc);
qsort(Value, 4, sizeof(float), cmpfunc2);
}

Does someone see a mistake, or know how to write the cmpfunctions? I only want to have the Value array sorted from lowest to highest. The real code, is hard to understand (i'm new to c) main.h :

typedef struct{
    float Value[5] ;
double Acal[5];
} value;

int cmpfunc();
int cmpfun2();

This is the Array, just dont care about the struct. My Array i want to sort with cmpfunc1 is a double (double Acal[5])

CodePudding user response:

It seems you're mixing up float and double.

This shows how to do the job. It sorts an array of double and an array of float once top down, and once bottom up.

It's rather self explanatory:

#include <stdio.h>
#include <stdlib.h>

// compare functions for top down sorting

int cmpfuncDoubleTopDown(const void* a, const void* b) {
  if (*(double*)a < *(double*)b)
    return 1;
  else if (*(double*)a > *(double*)b)
    return -1;
  else
    return 0;
}

int cmpfuncFloatTopDown(const void* a, const void* b) {
  if (*(float*)a < *(float*)b)
    return 1;
  else if (*(float*)a > *(float*)b)
    return -1;
  else
    return 0;
}


// compare functions for top bottom up sorting

int cmpfuncDoubleBottomUp(const void* a, const void* b) {
  if (*(double*)a > *(double*)b)
    return 1;
  else if (*(double*)a < *(double*)b)
    return -1;
  else
    return 0;
}

int cmpfuncFloatBottomUp(const void* a, const void* b) {
  if (*(float*)a > *(float*)b)
    return 1;
  else if (*(float*)a < *(float*)b)
    return -1;
  else
    return 0;
}


int main() {
  double dvalues[] = { 3.0, 1.0, 4.0, 2.0 };
  float fvalues[] = { 3.0f, 1.0f, 4.0f, 2.0f };

  // sort top down
  qsort(dvalues, 4, sizeof(double), cmpfuncDoubleTopDown);
  qsort(fvalues, 4, sizeof(float), cmpfuncFloatTopDown);

  for (int i = 0; i < 4; i  )
    printf("%f ", fvalues[i]);

  printf("\n");

  for (int i = 0; i < 4; i  )
    printf("%f ", dvalues[i]);

  printf("\n");

  // sort bottum up
  qsort(dvalues, 4, sizeof(double), cmpfuncDoubleBottomUp);
  qsort(fvalues, 4, sizeof(float), cmpfuncFloatBottomUp);

  for (int i = 0; i < 4; i  )
    printf("%f ", fvalues[i]);

  printf("\n");

  for (int i = 0; i < 4; i  )
    printf("%f ", dvalues[i]);

  printf("\n");
}
  • Related