#include<stdio.h>
int find(int x,int y[],int n);
int main(void)
{
int A[]={1,2,3,4,5,6,9};
int b=12;
int f;
f=find(b,A,c);
if(f==-1)
printf("Can not find element b in arry A[]\n");
else
printf("Find element b in arry A[]:the %dth element\n",f);
return 0;
}
//折半查找法,用代查元素的个数与有序数组的中间元素进行比较,
//若是大于中间的元素,则在后一半继续查找,否则在前一半进行查找
int find(int x,int y[],int n)
{
int mid;
int low=0;
int high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(x<y[mid])
high=mid-1;
else if(x>y[mid])
low=mid+1;
else
return mid+1;
}
return -1;
}