#include<stdio.h>
#include<stdlib.h>
#define Max 11
typedef struct{
int data;
}A;
typedef struct{
A *elem;
int length;
}B;//数组建立顺序表
void init(B &L){
L.elem=(A*)malloc(sizeof(A)*Max);
L.length=0;
printf("init_success!\n");
printf("input the length:");
scanf("%d",&L.length);
printf("input some data:\n");
for(int i=1;i<=L.length;i++){
scanf("%d",&L.elem[i].data);
}
}
void insert(B &L){
for(int i=2;i<=L.length;i++){
L.elem[0]=L.elem[i];
int low=1;
int high=i-1;
int mid=(low+high)/2;
while(low<=high){
if(L.elem[0].data<L.elem[mid].data){
high=mid-1;
}
else if(L.elem[0].data>L.elem[mid].data){
low=mid+1;
}
else if(L.elem[0].data==L.elem[mid].data){
break;
}
mid=(low+high)/2;
}
int j;
for(j=i-1;j>=low;j--)
L.elem[j+1]=L.elem[j];
L.elem[low]=L.elem[0];
}
}
void show(B L){
for(int i=1;i<=L.length;i++){
printf(" %d ",L.elem[i].data);
}
}
int main(){
B L;
init(L);
insert(L);
show(L);
}
c语言数据结构---折半排序(顺序查找表)
最新推荐文章于 2024-11-03 22:17:06 发布