#include <iostream>
using namespace std;
// 要排序a[0...i]
void insertSort(int a[], int i)
{
if (i <= 0) return ;
// a[0...i - 1]
// 执行完这句后, a[0...i - 1]已经是有序的
insertSort(a, i - 1);
// 然后把a[i]插入到a[0...i - 1]中
int key = a[i];
int j = i - 1;
while (j >= 0 && key < a[j])
{
a[j + 1] = a[j];
j --;
}
a[j + 1] = key;
}
int main()
{
int a[] = {1, 6, 7, 3, 4};
insertSort(a, 5);
for (int i = 0; i < 5; ++ i) cout << a[i] << " ";
cout << endl;
return 0;
}
02-08
705