Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
STD C快速排序中可能存在的错误。我错过什么了吗?_C - Fatal编程技术网

STD C快速排序中可能存在的错误。我错过什么了吗?

STD C快速排序中可能存在的错误。我错过什么了吗?,c,C,xorshift初始化:7731 A: 2064221648 1036493097 633233112 583013546 721278080 -1646392714 -829660162 478401127 E: 583013546 633233112 721278080 1036493097 2064221648 -1646392714 -829660162 478401127 预期: -1646392714 -829660162 478401127 583013546 633233112

xorshift初始化:7731

A: 2064221648 1036493097 633233112 583013546 721278080 -1646392714 -829660162 478401127

E: 583013546 633233112 721278080 1036493097 2064221648 -1646392714 -829660162 478401127

预期: -1646392714 -829660162 478401127 583013546 633233112 721278080 1036493097 2064221648

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

#define debug (0 || (sz < 50))

int cmpfunc(const void* a, const void* b)
{
    int x = *(int*)a;
    int y = *(int*)b;
    return x-y;
}

unsigned int xorshift32(unsigned int x)
{
    /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    return x;
}

int
main()
{
    #define sz 8

    int* a = (int*)malloc(4*sz);

    srand(time(NULL));
    unsigned int init = rand();   printf("xorshift init: %lld\n",init);

    int z=0;
    for(int i = sz-1; i>=0;i+=0)
    {
        a[z] = xorshift32(init) % 0xD0000000U; init = a[z];

        z++;
        i--;
    }
    if(debug)
    {
        printf("A:\n");
        for(int i = 0; i<sz;i++)
        {
            printf("%11d\n", a[i]);
        }printf("\n");
    }

    qsort(a,sz,4,cmpfunc);

    printf("E: \n");
    if(debug)
    {
        for(int i = 0; i<sz;i++)
        {
            printf("%11d\n", a[i]);
        }printf("\n");
    }
}
#包括
#包括
#包括
#定义调试(0 | |(sz<50))
int cmpfunc(常数无效*a,常数无效*b)
{
int x=*(int*)a;
int y=*(int*)b;
返回x-y;
}
无符号整数xorshift32(无符号整数x)
{
/*Marsaglia第4页的“xor”算法,“Xorshift RNGs”*/
x^=x>17;
x^=x=0;i+=0)
{
a[z]=xorshift32(init)%0xd000000u;init=a[z];
z++;
我--;
}
如果(调试)
{
printf(“A:\n”);
对于(int i=0;i

gcc sort.c -Wall -Wextra
有一个关于不匹配转换说明符的错误(
unsigned int
需要
%u
,但您有
%lld
)-可能是
%11d
的打字错误,但即使如此,它也是错误的

运行时,我得到的输出有时是正确的,有时是不正确的。因此,我使用
-fsanize=undefined
编译,并且

sort.c:11:13: runtime error: signed integer overflow: 
    1288106901 - -1003011281 cannot be represented in type 'int'
E: 
  290879035
  591885416
  767444883
 1288106901
 1955087149
-1509681722
-1289472872
-1003011281
例如,您的智能代码不太智能。从比较函数返回值的正确方法是

return x < y ? -1 : x > y ? 1 : 0;
返回xy-1:0;

返回(x>y)-(x

正如

“xorshift init:%lld\n”
(注意
%lld
)->
“xorshift init:%11d\n”
如果每次都使用相同的随机种子(而不是
时间(NULL)
),调试事情会更容易.总的来说,如果你有一些自己编写的代码,以及数百万个程序多年来使用的标准库函数,并且两者的结合会产生奇怪的结果,那么通常建议你在自己的代码中查找错误。
for(int i=sz-1;i>=0;i+=0){a[z]=…;z++;i--;}
这看起来可疑,为什么不为(intz=0;z
?@HolyBlackCat-hmm事实上,godbolt显示我的导致用GCC生成一个分支,而你的没有。或者
返回xy;
。\n 1:0
几乎总是多余的。@rici确实,我只是让它更明确。不过,GCC也会为此生成一个分支:/@antti:除非你要求优化se或类似的激进的东西。@FrantzelasG顺便说一句,正确的页面应该是;)它也有这个信息。
return (x > y) - (x < y);