起泡排序的疑惑
为何第一段程序执行后结果会报错,第二段却没事呢?该怎么改?谢谢//用起泡法对10个数进行由小到大排列
#include <stdio.h>
#define N 10
int main()
{
int num[N],i,j,smaller;
printf("input 10 numbers:\n");
for (i=0;i<N;i++)
scanf("%d",&num[i]);
for (j=0;j<N-1;j++) //N个数循环N-1趟
for (i=0;i<N-j;i++) //第j趟进行N-j次比较
if (num[i]>num[i+1])
{ smaller=num[i];
num[i]=num[i+1];
num[i+1]=smaller;
}
printf("the number from low to high is : \n ");
for (i=0;i<N;i++)
printf(" %4d",num[i]);
printf("\n");
return 0;
}
//用起泡法对10个数进行由小到大排列
#include <stdio.h>
#define N 10
int main()
{
int num[N+1],i,j,smaller;
printf("input numbers:\n");
for (i=1;i<=N;i++)
scanf("%d",&num[i]);
for (j=1;j<N;j++) //N个数循环N-1趟
for (i=1;i<=N-j;i++) //第j趟进行N-j次比较
if (num[i]>num[i+1])
{ smaller=num[i];
num[i]=num[i+1];
num[i+1]=smaller;
}
printf("the number from low to high is : \n ");
for (i=1;i<=N;i++)
printf(" %4d",num[i]);
printf("\n");
return 0;
}