以下代码是为排序数组编写的。
这里程序给出了意想不到的输出结果。
错误应该在10-19行。试图创建排序程序,但未按预期工作
#include<stdio.h> //Sorting array program
int main()
{
int arr[20],i,j,n,temp;
printf("Enter number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements of the array : ");
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<arr[i];j++)
if (arr[i+1]<arr[i])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
printf("\nThe sorted list is : \n");
for (i=0;i<n;i++)
printf("\n arr[%d] : %d",i,arr[i]);
return 0;
}
输出:: 输入要素数:4
Enter the elements of the array : 5
3
2
1
The sorted list is :
arr[0] : 3
arr[1] : 2
arr[2] : 1
arr[3] : -16777216
Process returned 0 (0x0) execution time : 8.161 s
Press ENTER to continue.
闻起来像一个“索引越界”的错误。 –
这是哪种排序算法?我对内循环有怀疑。 – Gaurav
'j
BLUEPIXY