CH7_2
上传用户:lgb298
上传日期:2013-03-22
资源大小:1025k
文件大小:1k
- #include <stdio.h>
- #define M 500
- typedef struct
- { int key;
- /* float info;*/
- }JD;
- int binsrch(JD r[],int n,int k)
- { int low,high,mid,found;
- low=1; high=n; found=0;
- while((low<=high)&&(found==0))
- { mid=(low+high)/2;
- if(k>r[mid].key) low=mid+1;
- else if(k==r[mid].key) found=1;
- else high=mid-1;
- }
- if(found==1)
- return(mid);
- else
- return(0);
- }
- void main()
- {
- static JD r[]={0,5,13,19,21,37,56,64,75,80,88,92};
- int i,n=11;
- int key;
- printf("Input the key you want to search:");
- scanf("%d",&key);
- i=binsrch(r,n,key);
- if(!i)
- printf("Not foundn");
- else
- printf("the index of %d is %dn",key,i);
- }