C qsort not working correctly

后端 未结 4 2164
北荒
北荒 2020-12-16 16:42

I don\'t know what I\'m doing wrong but the following code does not sort the array properly.

#include 
#include 

int compare(         


        
4条回答
  •  误落风尘
    2020-12-16 17:15

    I'm giving a code example using the information above. In my compiler and system, I get the same results as Ram who asked the question. This indicates that my integers are something like Ram's integers. I modified my code along the lines suggested by Mehrdad to use comparison operators instead of a subtraction. Then I got the numbers sorted properly.

    Here is the code:

        #include 
        #include 
    
        int compare(const void* a, const void* b)
        {
            int
                n1 = * (int *) a,
                n2 = * (int *) b;
    
            /*
            Usine the ternary to express along the lines of
                if
                elseif
                elseif
                .
                .
                .
                else
            */
    
            return 
                n1 > n2             // if
                ? 1                 // then
                : n1 == n2          // else if
                ? 0                 // then
                : -1                // else
                ;                   // end if
        }
    
        int main(int argc, char * argv[])
        {
            int x[] = 
            { 
                -919238029, -889150029, -826670576, -579609061, -569653113, -305140505, -216823425, -193439331,
                -167683147, -49487019,  -45223520,  271789961,  275570429,  444855014,  559132135,  612312607,
                664554739,  677860351,  1005278191, 1031629361, 1089012280, 1115952521, 1521112993, 1530518916,
                1907515865, 1931470931, -1631034645,-1593702794,-1465300620,-1263094822
            };
    
            int 
                i = 0,                          // index
                imax = sizeof(x)/sizeof(int);   // max value for index
    
            FILE * outf = 0;
    
            if ( !(outf = fopen("output.txt", "wt")) )
            {
                puts("outf == 0 which is an error trying to open \"output.txt\" for writing.\n");
                getch();
                return;
            }
    
            qsort(x, imax, sizeof(int), compare);
    
    
            for(i = 0; i < imax; i ++)
                fprintf(outf, "%d\n", x[i]);
    
            fclose(outf);
    
            return 0;
        }
    

    And I get this output:

    -1631034645
    -1593702794
    -1465300620
    -1263094822
    -919238029
    -889150029
    -826670576
    -579609061
    -569653113
    -305140505
    -216823425
    -193439331
    -167683147
    -49487019
    -45223520
    271789961
    275570429
    444855014
    559132135
    612312607
    664554739
    677860351
    1005278191
    1031629361
    1089012280
    1115952521
    1521112993
    1530518916
    1907515865
    1931470931
    

提交回复
热议问题