int bin_search(int a[],int len,int key)
{
int low=0;
int high=len-1;
while(low<=high) //若为low<high; e.g.1,2,3,4,5 如果search 5 ,最终 low==high=4; 而循
//体 不执行。
{
int mid=low+(high-low)/2;
if(a[mid]==key) return mid;
else
if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
if(low>high)
return -1;
}
时间: 2024-10-11 02:44:56