#include<stdio.h>
#include <stdlib.h>
int main()
{
int ret=0;
int arr[]={1,2,5,6,88,99,456,666};
ret= binsearch(666,arr,0,sizeof(arr) / sizeof(arr[0])-1);
if(ret!=-1)
{
printf("%d",ret);
}
else
{
printf("not exist");
}
return 0;
}
int binsearch(int x,int arr[],int left,int right)
{
while(left<=right)
{
int mid=left-(left-right)/2;
if(x<arr[mid])
{
right=mid-1;
}
else if(x>arr[mid])
{
left=mid+1;
}
else
{
return arr[mid];
}
}
return -1;
}
时间: 2024-10-09 06:49:04