#include <stdio.h>
#define n 100
typedef int keytype;
typedef struct{
keytype key;
}nodetype;
typedef nodetype seqlist[n+1];
seqlist r;
main(){
int m,i,j;
keytype k;
for(i=1;i<=n;i++) r[i].key=i;//ɵ1~n
for(i=1;i<=n;i++) //ҵ
printf("%d--%d ",i,r[i].key);
printf("/n");
printf("input k:/n");//Ҫҵֵk
scanf("%d",&k);
printf("search position is:%d/n",binsearch(r,k));//۰Ҳk
}//end of main
int binsearch(seqlist r,keytype k)
{//۰
int low=1,high=n,mid;
while (low<=high){
mid=(low+high)/2;
if (r[mid].key==k) return mid;
if (r[mid].key>k)
high=mid-1;
else
low=mid+1;
}
return 0;
}
折半查找
最新推荐文章于 2022-11-03 20:44:43 发布