[英]C qsort not working correctly
我不知道我做错了什么,但下面的代码没有正确排序数组。
#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;
}
产生以下 output:
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
我的意思是,问题/必须/在我的比较 function 中。 有人注意到什么奇怪的吗?
是的,你的“比较”溢出了。 :(
当你从一个正数中减去一个负数时,你的结果不一定是正数; 如果它不能在数据类型中表示,它将“环绕”另一侧。
如果您的 integer 只能保持 -8 到 7(4 位),那么当您比较 4 到 -4 时会发生什么?
好吧,你得到 8,即二进制的1000
,即 -8。 所以 4 小于 -4。
不要做减法而不是比较,即使他们在学校告诉你“看看这有多酷”!
在一般情况下,您不能使用减法来比较整数。 或者,更准确地说,您可以,但仅在您确定减法不会溢出的情况下。 在您的情况下,减法溢出,产生完全没有意义的结果(甚至没有提到当签名的 integer 减法溢出时,行为是未定义的)。
用于在值a
和b
之间生成三态 C 风格比较的常用习语是(a > b) - (a < b)
表达式。 它适用于几乎任何可比类型的数据。 在您的情况下,比较 function 可能如下所示
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;
}
我得到了这个 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
要添加到 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
而不是x < z
。 要进一步调试此问题,请运行
export SORTCHECK_OPTIONS=raise=1
并检查生成的编码转储。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.