#include<iostream>
using namespace std;
/*
折半插入排序:
时间复杂度为O(n^2) ,其中A[0] 位置不放元素,作为哨兵
*/
typedef int ElemType;
void InsertSort(ElemType A[],int n){
int i,j,low,high,mid;
int temp;
for(i = 1;i < n;i++){ //依次将A[1]~A[n]插入前面的已经排好的序列
temp= A[i]; //将A[i]暂存到temp中
low = 0;high = i-1; //设置折半查找范围
while(low <= high){ //折半查找(默认递增序列)
mid = (low + high)/2; //取中间点
if(A[mid] > temp){
high = mid - 1; //查找左半子表
}
else{
low = mid + 1; //查找右半子表
}
}
for(j = i;j > high + 1;j--){
A[j] = A[j-1]; //统一后移元素,空出插入位
}
A[high + 1] = temp; //插入操作
}
}
int main(){
ElemType A[]={10,25,13,1,24,27,15,21};
InsertSort(A,8);
for(int i= 0;i < 8;i++){
cout<<A[i]<<" ";
}
return 0;
}