插入排序(Insertion Sort)的基本思想是:每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子序列中的适当位置,直到全部记录插入完成为止。
//插入排序,从小到大
void InsertSort(int a[], int n){
for (int i = 1; i < n; i++){
int temp = a[i];
for (int j = i; j >0 && temp < a[j - 1]; j--){
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}