使用冒泡排序的方法对一组数据进行排列,当我让j=1;j<=10-i时,代码运行报错,但当改成j=0;j<=9-i时,代码就能运行成功,请问这是为什么?
#include<stdio.h>
int main()
{
int arr[10] = { 21,31,64,75,37,9,94,3,12,58 };
printf("排序前的序列:\n");
for (int k = 0; k < 10; ++k)
{
printf("%d ", arr[k]);
}
for (int i = 1; i < 10; ++i)
{
for (int j = 1; j <= 10- i; ++j)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("\n");
printf("排序后的序列:\n");
for (int k = 0; k < 10; ++k)
{
printf("%d ", arr[k]);
}
return 0;
}
报错内容:
Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.
运行时检查失败# 2 -变量' a rr '周围的堆栈损坏。