Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
C中返回长整数的冒泡排序_C_Bubble Sort - Fatal编程技术网

C中返回长整数的冒泡排序

C中返回长整数的冒泡排序,c,bubble-sort,C,Bubble Sort,我在main中定义了int数组,后面是冒泡排序的函数调用: int numarr[6] = { 6, 4, 3, 5, 1, 2 }; int arrsize = 6; bubblesort(&numarr[0], arrsize); 我的冒泡排序函数如下所示: int bubblesort(int num[], int x) { int temp, j, i; for (j = 0; j < x; j++ ) { for (i = 0; i

我在main中定义了int数组,后面是冒泡排序的函数调用:

int numarr[6] = { 6, 4, 3, 5, 1, 2 };
int arrsize = 6;

bubblesort(&numarr[0], arrsize);
我的冒泡排序函数如下所示:

int bubblesort(int num[], int x) {
    int temp, j, i;

    for (j = 0; j < x; j++ ) {
        for (i = 0; i < x - j; i++) {
            if (num[i] > num[i + 1]) {
                temp = num[i];
                num[i] = num[i + 1];
                num[i + 1] = temp;
            }
        }
    }
    return 0;
}
如果你想知道,我在函数调用后跟踪了它,如下所示:

printf(" %d,", num[0]);
printf("%d, ", num[1]);

以此类推。

i=x-1
时,
num[i+1]
超出范围,不能访问


i=x-1
num[i+1]
超出范围且不能访问时,尝试使用
for(j=1;j
代替
for(j=0;j


尝试对(j=1;j而不是对(j=0;j

以下是气泡排序的正确算法

请注意posted代码算法与以下算法之间的显著差异。特别注意
for()
语句中索引变量
c
d
的限制:

void bubblesort( int num[], int x)
{
    int c;
    int d;
    int swap;


    for (c = 0 ; c < ( x - 1 ); c++)
    {
        for (d = 0 ; d < (x - c - 1); d++)
        {
            if (num[d] > num[d+1]) /* For decreasing order use < */
            {
                swap     = num[d];
                num[d]   = num[d+1];
                num[d+1] = swap;
            }
        }
    }
}
void bubblesort(int num[],int x)
{
INTC;
int d;
国际互换;
对于(c=0;c<(x-1);c++)
{
对于(d=0;d<(x-c-1);d++)
{
如果(num[d]>num[d+1])/*用于降序使用*/
{
swap=num[d];
num[d]=num[d+1];
num[d+1]=交换;
}
}
}
}

以下是气泡排序的正确算法

请注意posted代码算法与以下算法之间的显著差异。特别注意
for()
语句中索引变量
c
d
的限制:

void bubblesort( int num[], int x)
{
    int c;
    int d;
    int swap;


    for (c = 0 ; c < ( x - 1 ); c++)
    {
        for (d = 0 ; d < (x - c - 1); d++)
        {
            if (num[d] > num[d+1]) /* For decreasing order use < */
            {
                swap     = num[d];
                num[d]   = num[d+1];
                num[d+1] = swap;
            }
        }
    }
}
void bubblesort(int num[],int x)
{
INTC;
int d;
国际互换;
对于(c=0;c<(x-1);c++)
{
对于(d=0;d<(x-c-1);d++)
{
如果(num[d]>num[d+1])/*用于降序使用*/
{
swap=num[d];
num[d]=num[d+1];
num[d+1]=交换;
}
}
}
}

像这样疯狂的数字通常表示你已经超出了数组的范围。尝试在每个步骤中打印i和i+1(或在调试器中查看它们),查看是否出现大于5的数字。在第二个循环中尝试x-j-1这是一个很好的学习示例,但一旦您了解了控制流和数组索引的工作原理,请永远忘记冒泡排序,因为这对于实际工作来说是一个糟糕的算法。最终你会想学习如何使用
qsort
。实际上,对于小数组(比如少于10个条目),冒泡排序、插入排序等都是很好的选择。但是,当数组大小扩展到超过10个条目时,进行比较的次数会使冒泡排序等的使用成本更高。这就是qsort、mergesort、sieve排序等真正开始发光的时候。像这样疯狂的数字通常是你已经超出了数组的某个范围的标志。尝试在每个步骤中打印i和i+1(或在调试器中查看它们),查看是否出现大于5的数字。在第二个循环中尝试x-j-1这是一个很好的学习示例,但一旦您了解了控制流和数组索引的工作原理,请永远忘记冒泡排序,因为这对于实际工作来说是一个糟糕的算法。最终你会想学习如何使用
qsort
。实际上,对于小数组(比如少于10个条目),冒泡排序、插入排序等都是很好的选择。但是,当数组大小扩展到超过10个条目时,进行比较的次数会使冒泡排序等的使用成本更高。这就是qsort、mergesort、sieve-sort等真正开始出现的时候。非常感谢您的解释,这几乎解决了它。您的数组被声明为
numar
,稍后被称为
num
。不管怎样,你不需要“地址”和索引,只要调用
bubblesort(numarr,arrsize)
非常感谢您的解释,这几乎解决了问题。您的数组被声明为
numar
,稍后被称为
num
。不管怎样,你不需要“地址”和索引,只要调用
bubblesort(numarr,arrsize)