二分法查找
#include<stdio.h>
int main()
{
int f=8;
int arr[]={1,2,3,4,5,6,7,8,9,10};
int left=0;
int right=sizeof(arr)/sizeof(arr[0])-1;
while(left<=right)
{
int mid=(left+right)/2;
if(arr[mid]<f)
{
left=mid+1;
}
else if(arr[mid]>f)
{
right=mid-1;
}
else
{
printf("找到了,其下标为%d\n",mid);
break;
}
}
if(left>right)
{
printf("没找到该元素\n");
}
return 0;
}
标签:arr,right,int,之路,mid,else,left From: https://blog.51cto.com/u_16248324/7570610