C qsort无法正常工作

| 我不知道自己在做什么错,但是以下代码无法正确对数组进行排序。
#include <stdio.h>
#include <stdlib.h>

int compare(const void* a, const void* b)
{
    return (*(int*)a - *(int*)b);
}

int main()
{
    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;

    qsort(x, 30, sizeof(int), compare);
    for(i = 0; i < 30; i ++)
        printf(\"%d\\n\", x[i]);

    return 0;
}
产生以下输出:
1521112993
1530518916
1907515865
1931470931
-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
我的意思是,问题/必须/在我的比较功能中。有人注意到什么奇怪的事吗?     
已邀请:
是的,您的“比较”溢出了。 :( 原因: 当您从正数中减去负数时,结果不一定是正数。如果它不能用数据类型表示,它将在另一侧“环绕”。 例: 如果您的整数只能保留-8到7(4位),那么当您将4与-4比较时会发生什么? 好了,您得到8,即二进制中的
1000
,即-8。因此4小于-4。 道德: 即使他们在学校告诉您“看这有多酷”,也不要做减法而不是比较!     
通常,您不能使用减法来比较整数。或者,更确切地说,您可以,但是仅在您确定减法不会溢出的情况下。在您的情况下,减法溢出会产生完全没有意义的结果(甚至没有提到当有符号整数减法溢出时行为是不确定的)。 生成值
a
b
之间的三态C风格比较的常见习惯用法是
(a > b) - (a < b)
表达式。它适用于几乎所有可比较类型的数据。在您的情况下,比较功能可能如下所示
int compare(const void* a, const void* b)
{
  int va = *(const int*) a;
  int vb = *(const int*) b;
  return (va > vb) - (va < vb);
}
    
我使用上面的信息给出一个代码示例。在我的编译器和系统中,我得到的结果与Ram提出问题的结果相同。这表明我的整数类似于Ram \的整数。我按照Mehrdad的建议修改了代码,以使用比较运算符而不是减法。然后我得到了正确排序的数字。 这是代码:
    #include <stdio.h>
    #include <stdlib.h>

    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;
    }
我得到以下输出:
-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
    
要添加到Mehrad的正确答案中,这是一种使用SortChecker在代码中查找错误的自动方法:
$ LD_PRELOAD=$HOME/sortcheck-master/bin/libsortcheck.so ./a.out
a.out[38699]: qsort: comparison function is not transitive (comparison function 0x40057d (/home/iuriig/a.out+0x40057d), called from 0x400693 (/home/iuriig/a.out+0x400693), cmdline is \"./a.out\")
-919238029
-889150029
...
该警告表明
compare
报告
x < y, y < z
,而非某些输入ѭ12not。要进一步调试此问题,请运行
export SORTCHECK_OPTIONS=raise=1
并检查生成的代码转储。     

要回复问题请先登录注册