折半插入排序
#include<stdio.h>
#define MAXSIZE 1024
typedef struct
{
int key;
char data;
}RecordType;
int B_insertSort(RecordType R[],int n)
{
int i,j,low,high,mid;
for(i=2;i<=n;i++)
{
R[0]=R[i];
low=1,high=i-1;
while(low<=high)
{
mid=(low+high)/2;
if(R[0].key>R[mid].key)
low=mid+1;
else
high=mid-1;
}
for(j=i-1;j>=high+1;j--)
R[j+1]=R[j];
R[high+1]=R[0];
}
}
int main()
{
int i=1,j,x;
RecordType R[MAXSIZE];
printf("Input data of list(-1 stop):\n");
scanf("%d",&x);
while(x!=-1)
{
R[i].key=x;
scanf("%d",&x);
i++;
}
printf("Output data in list:\n");
for(j=1;j<i;j++)
printf("%4d",R[j].key);
printf("\nSort:\n");
B_insertSort(R,i-1);
printf("\nOutput data in list after Sort:\n");
for(j=1;j<i;j++)
printf("%4d",R[j].key);
printf("\n");
}