思路:
插入排序基本思想是每一步将一个待排序的记录,插入到前面已经排好序的有序序列中去,直到插完所有元素为止。
代码:
#include<iostream>
using namespace std;
int main() {
int a[6] = { 2, 6, 5, 3, 4, 1};
int temp, i, j;
int n = 6;
for (i = 1; i < 6; i++) {
temp = a[i]; j = i;
while (j >= 1 && temp < a[j - 1]) {
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
for (i = 0; i < 6; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}