//直接插入排序,时间复杂度为O(n^2)
void InsertSort(int* p){
int guard;//哨兵
for(int i = 1;i < p.length;++i){
if(compare(p[i],p[i-1])){
guard = p[i];
p[i] = p[i-1];
for(int j = i-2;compare(guard,p[j]);--j){
p[j+1] = p[j];
}
p[j+1] = guard;
}
}
}
//折半插入排序,时间复杂度为O(n^2)
void BInsertSort(int* p){
for(int i = 1;i < p.length;i++){
if(compare(p[i],p[i-1])){
int guard = p[i];
int low = 0,high = i-1,m;
while(low <= high){//用折半查找的办法查找插入的位置
m = (low+high)/2;
if(compare(guard,p[m]))
high = m - 1;
else
low = m + 1;
}
for(int j = i-1;j >= high+1;--j){
p[j+i] = p[j];
}
p[high+1] = guard;
}
}
}