Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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 - Fatal编程技术网

C 小数据完美,大数据错误:一个奇怪的冒泡排序问题

C 小数据完美,大数据错误:一个奇怪的冒泡排序问题,c,C,我正在写一个冒泡排序程序。 我使用TCC(http://bellard.org/tcc/)。 我在程序中使用长变量,因为输入数据非常大。 我的问题是:当输入数据的数量很小(例如10个)时,我的程序工作得很好。但是当输入数据的数量很大(例如5814)时,我的程序工作错误 ========================================= 我的代码和输入数据文件在这里: ========================================= 这是我的程序和测试数据: /*

我正在写一个冒泡排序程序。 我使用TCC(http://bellard.org/tcc/)。 我在程序中使用长变量,因为输入数据非常大。 我的问题是:当输入数据的数量很小(例如10个)时,我的程序工作得很好。但是当输入数据的数量很大(例如5814)时,我的程序工作错误

=========================================
我的代码和输入数据文件在这里:

=========================================

这是我的程序和测试数据:

/*bubble.c*/
#include <stdio.h>

int main()
{
    int n,i,j;
    long long t,
        a[6001];    /*Change this to a[10000], then it works perfectly*/

    freopen("data.in.txt","r",stdin);
    freopen("date.out.txt","w",stdout);
    scanf("%d",&n);

    /*Read input data from "data.in.txt"*/
    for (i=1;i<=n;i++) {
        scanf("%lld",&a[i]);
        /*printf("i=%d\ta[i]=%lld\n",i,a[i]);*/
    }

    /*Bubble Sort*/
    for (i=1;i<=n-1;i=i+1) {
        for (j=n;j>=i+1;j=j-1) {
            if (a[j]<a[j-1]) {
                t=a[j];
                a[j]=a[j-1];
                a[j-1]=t;
            }
        }
    }

    /*Output data to "data.out.txt"*/
    for (i=1;i<=n;i++) {
      printf("i=%d\ta[i]=%lld\n",i,a[i]);
    }


    /*printf("Time used =%lf\n",(double)clock() / CLOCKS_PER_SEC);*/
    /*system("pause");*/
    return 0;
}
/*bubble.c*/
#包括
int main()
{
int n,i,j;
长长的,长长的,
a[6001];/*将此更改为a[10000],则它可以完美工作*/
freopen(“data.in.txt”,“r”,stdin);
freopen(“date.out.txt”,“w”,stdout);
scanf(“%d”和“&n”);
/*从“data.in.txt”读取输入数据*/

对于(i=1;i我刚刚下载了您的文件,并用GCC 4.1.2和TCC 0.9.24编译了它,您的程序在这两个编译器中都能正常运行。输出匹配right.date.out.txt,因此您的代码是正确的。也许您的环境中还有其他原因导致编译或运行程序时出现问题。

C中的数组来自
0
n-1
。你写循环之类的东西的方式非常不标准,很难遵循。
scanf()
返回一个值,指示应用了多少次转换。请使用该值。如果该值不是预期值,则表示数据中有错误。请发布编译的真实源代码,而不是declared@CharlesB:事实上,它是(但确实不可读):前一行以一个
…结束。这只是一个想法,但是为什么不在知道数组有多大后再创建它呢?与其根据需要将其设置得太大,不如读入数字并将数组大小设置为该值。