Possible Duplicate:
Small data perfect, Large data wrong: A strange bubble sort question.
我的代码和输入数据文件在这里:
https://skydrive.live.com/?cid=bfe8af46e42e3ecf&sc=documents&id=BFE8AF46E42E3ECF!440#cid=BFE8AF46E42E3ECF&id=BFE8AF46E42E3ECF!935&sc=documents
我正在编写气泡排序程序。
我使用TCC(http://bellard.org/tcc/)。
我在程序中使用了long long变量,因为输入数据非常大。
我的问题是:当输入数据的数量很少(例如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;
}
================================
我的输入数据:非常大的数字。
5814
209442427
1519418927 828028199 47874386 1918308053 665370647 355436872 122922452 1361311685 1711685536 1850886562 752723777 567058321 1879534287 579940183 1802179021 2004892116 1219034394 269237342 410745567 849113437 ......
最佳答案
你为什么用
scanf("%lld",&n);
当
n
声明为int
时?这可能会覆盖内存。关于c - 小数据完美,大数据错误:一个奇怪的气泡排序问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6706500/