#include<stdio.h>
#define N 8
void binsearch(int a[]);
void show(int a[]);
int main()
{
int a[N] = {50,36,66,76,95,12,25,36};
printf(“原无序记录:\n”);
show(a);
printf(“排序过程如下\n”);
binsearch(a);
return 0;
}
void show(int a[])
{
int i;
for(i = 0;i < N;i++)
{
printf(“%d\t”,a[i]);
}
puts(“”);
}
void binsearch(int a[])
{
int i,j,tmp,low,high,mid;
for(i = 1;i < N;i++)
{
tmp = a[i];//无序记录保存temp中 有序记录中折半查找插入位置
for(low = 0,high = i -1;low <= high;)
{
mid = (low + high)/2;
if(tmp < a[mid])
high = mid -1;
else
low = mid + 1;
}
//将从low开始的有序记录向后移动一个位置
for(j = i;j > low;j–)
{
a[j] = a[j – 1];
}
a[low] = tmp;
show(a);
}
}