- 论坛徽章:
- 0
|
运行环境:TCC http://bellard.org/tcc/
我的源代码和输入数据文件,可以在这里下载:https://skydrive.live.com/?cid=B ... 35&sc=documents
======================
问题描述:
1、输入数据文件中,共有5814个整数,每个整数都很大,所以我用了一个64位的长整型数组进行排序。
2、我用的是冒泡排序。
3、如果排序所用的数组a的大小为a[6001],则排序结果有误;但如果改为a[10000](改大些),就可以正常工作了。
这是为什么呢?我觉得我的代码明显没有出现数组越界的问题啊!
另外,我的代码在Dev C++下也可以正常编译运行,但也会产生同样的问题。
请指点!谢谢!
代码如下:- /*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;
- }
复制代码 |
|