#include <iostream.h>
#include <stdio.h>
void binsertsort(int r[] ,int n)
{
int i,j;
int low,high;
int m;
for(i=2;i<=n;i++)
r[0]=r[i]; // 将r[i]暂存到 r[0]
low=1;
high=i-1;
while(low<=high)
{
m=(low+high)/2 ;
{
if(r[0]<r[m])
high=m-1;
else low=m+1;
}
for(j=i-1;j>=high+1;j--)
r[j+1]=r[j];
r[high+1]=r[0];
}
}
void print(int r[] ,int n)
{
int i;
for(i=0;i<n;i++)
printf("r[%d]=%d\n",i,r[i]);
}
void main()
{
int i,j;
int n;
int r[100];
scanf("%d",&n);
printf("要排序的数据个数%d为",n);
for(i=0;i<n;i++)
scanf("%d",&r[i]);
printf("插入前的数组为");
print(r,n);
binsertsort(r,n);
printf("插入后的数组为");
print(r,n);
}
//折半插入排序
template<typename T>
void BInsertSort(T array[],int n)
{
int i,j,mid;
int low,high;
T temp;
for (i=1;i<n;++i)
{
temp=array[i];
low=0;
high=i-1;
while (low<=high)
{
mid=(low+high)/2;
if (temp<array[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
for (j=i-1;j>=high+1;j--)
{
array[j+1]=array[j];
}
array[high+1]=temp;
}
}