我对5632078591这十个数字进行冒泡法排序,为什么进行VC++运行后的结果是这样的呢?下图所示,望大侠们指

Input 10 numbers:
5 6 3 2 0 7 8 5 9 1
The sorted numbers is :
-858993460-858993460-858993460-858993460-858993460 0 2 3 5 6
Press any key to continue
源程序是
#include<stdio.h>
void fun(int a[],int n) /*定义函数实现冒泡排序法*/
{
int i,j,temp;
temp=a[0];
for(i=1;i<n;i++) /*使用嵌套循环,实现第i趟循环*/
for(j=0;j<n-i;j++) /*实现第j次循环*/
if(a[j]>a[j+1]) /*如果前面的数大就交换*/
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
void main()
{
int i,str[10];
printf("Input 10 numbers:\n");
for(i=0;i<5;i++)
scanf("%d",&str[i]);
printf("\n");
fun(str,10); /*调用函数*/
printf("The sorted numbers is:\n");
for(i=0;i<10;i++) /*输出最后结果*/
printf("%2d",str[i]);
printf("\n");
}

//就改一行就正常了!
//仔细找这行:for(i=0;i<10;i++) //5改10!
#include<stdio.h>
void fun(int a[],int n) /*定义函数实现冒泡排序法*/
{
int i,j,temp;
temp=a[0];
for(i=1;i<n;i++) /*使用嵌套循环,实现第i趟循环*/
for(j=0;j<n-i;j++) /*实现第j次循环*/
if(a[j]>a[j+1]) /*如果前面的数大就交换*/
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
void main()
{
int i,str[10];
printf("Input 10 numbers:\n");
for(i=0;i<10;i++) //5改10!
scanf("%d",&str[i]);
printf("\n");
fun(str,10); /*调用函数*/
printf("The sorted numbers is:\n");
for(i=0;i<10;i++) /*输出最后结果*/
printf("%2d",str[i]);
printf("\n");
}
温馨提示:答案为网友推荐,仅供参考
相似回答