顺序查找和折半查找
#include
#define MAXLEN 50
typedef struct{
int key;
char data;
}datatype;
int getsort(datatype x[]);
int seqsearch(datatype x[],int key,int n);
int binsearch(datatype x[],int key,int n);
void insertsort(datatype x[],int n); void print(datatype x[],int n); void main()
{ int sel,n,s,key,i;
datatype x[MAXLEN];
printf("请输入你的选择:1---开始 其他选择为退出\nyour choice="); scanf("%d",&sel);
while(sel==1)
{ printf("输入关键字序号,以-1结束输入:");
n=getsort(x);
printf("请输入选择的查找方法:1---顺序查找 2---折半查找 3---退出查找\nyour
choice=");
scanf("%d",&s);
while(s!=3)
{ printf("请输入查找关键字给定值=");
scanf("%d",&key);
if(s==1)
{ printf("\n原关键字序列为:");
print(x,n);
i=seqsearch(x,key,n);
if(i==-1)
printf("\n没有给定值记录~\n");
else
printf("\n查找的结果为表中的第%d条记录~\n",i+1);
}
else if(s==2)
{ i=binsearch(x,key,n);
if(i==-1)
printf("\n没有给定值记录~\n");
else
printf("\n查找的结果为表中的第%d条记录~\n",i+1);
}
else
printf("选择错误选择~\n");
printf("请输入选择的查找方法:1---顺序查找 2---折半查找 3---退出查找
\nyour choice=");
scanf("%d",&s);
}
printf("请输入你的选择:1---开始 其他选择为退出\nyour choice=");
scanf("%d",&sel); }
}
int getsort(datatype x[]) { int i=0;
scanf("%d",&x[i].key);
while(x[i].key!=-1)
{ i++;
scanf("%d",&x[i].key);
}
return i;
}
int seqsearch(datatype x[],int key,int n)
{ int i;
for(i=0;iy[mid].key)
low=mid+1;
else
hig=mid-1;
}
return -1;
}
void insertsort(datatype x[],int n)
{ int i,j;
datatype temp;
for(i=1;i-1&&temp.key