二分查找(二分法)

#include <stdio.h>
int reserch(int a[],int left,int right,int x)
{
while(left<=right)
{
int mid=(left+right)/2;
if(a[mid]==x)
return mid;
else if(x>a[mid])
left=mid+1;
else if(x<a[mid])
right=mid-1;
}
return -1;

}
int main()
{
int a[10]={1,3,4,6,7,8,10,11,12,15};
printf("%d",reserch(a,0,9,2));
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_32631105/article/details/88750063