#include <iostream>
using namespace std;
int main(){
int a[6] = {5, 2, 4, 6, 1, 3}; // create an array of size 6
int j, key = 0;
for (int i = 1; i < 6; i++) {
key = a[i];
j = i - 1;
while ((j >= 0) && (a[j] > key)) {
a[j + 1] = a[j];
j -= 1;
}
a[j + 1] = key;
}
for (int l = 0; l < 6; l++) {
cout << a[l];
}
return 0;
}
我正在尝试使用代码符合的数组测试我的插入排序代码,但是当我尝试执行 a.out 文件时,它给了我“分段错误”,我查找了分段错误,这基本上是我们的错误正在尝试访问禁止的内存位置,但是,我想知道我的代码中的错误到底在哪里。另外,如果我摆脱
for (int l = 0; l < 6; l++) {
cout << a[l];
}
没有发现错误。