#include<stdio.h>
typedef struct
{
KeyType key;
char a;
}RecType;
void InsertSort(RecType R[],int n){
int i,j,low,high,mid;
RecType temp;
for(i=1;i<n;i++){
temp=R[i];
low=0;
high=i-1;
while(low<=high){
mid=(low+high)/2;
if(temp.key<R[mid].key)
high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=high+1;j--){
R[j+1]=R[j];
}
R[high+1]=temp;
}
}
void main(){
int i;
RecType R[5]={{9,'s'},{2,'f'},{4,'d'},{5,'a'},{1,'g'}};
InsertSort(R,5);
for(i=0;i<5;i++){
printf("%d\t",R[i].key);
printf("%c\n",R[i].a);
}
/*折半插入排序
插入排序的基本思想是:每次将一个待排序的元素,按
其关键字大小插入到已经排好序的子表中的适当位置,
直到全部元素插入完成为止*/
typedef struct
{
KeyType key;
char a;
}RecType;
void InsertSort(RecType R[],int n){
int i,j,low,high,mid;
RecType temp;
for(i=1;i<n;i++){
temp=R[i];
low=0;
high=i-1;
while(low<=high){
mid=(low+high)/2;
if(temp.key<R[mid].key)
high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=high+1;j--){
R[j+1]=R[j];
}
R[high+1]=temp;
}
}
void main(){
int i;
RecType R[5]={{9,'s'},{2,'f'},{4,'d'},{5,'a'},{1,'g'}};
InsertSort(R,5);
for(i=0;i<5;i++){
printf("%d\t",R[i].key);
printf("%c\n",R[i].a);
}
}