// InsertSort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
void swap(int (&a),int (&b)){
int temp = a;
a = b;
b = temp;
}
void insertSort(int *A){
for (int j = 1; j <= 6; j++){
for (int i = j-1; i>=0; i--){
if (A[i] >= A[i+1]){
swap(A[i], A[i+1]);
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int A[7] = { 31, 41, 59, 26, 41 };
insertSort(A);
system("pause");
return 0;
}
其实就是黑书的练习题
注意到的是,因为需要一个位置来防止边界溢出,故排序时申请的数组和数据元素相差为一,即需要额外的O(1)空间