关于二分查找的代码实现
#include<stdio.h>
int find_second(int arr[], int k, int left, int right)
{
while(left<=right)
{
int mid=left-(left-right)/2;
if(arr[mid]>k)
{
right=mid-1;
}
else if(arr[mid]<k)
{
left=mid+1;
}
else
{
return mid;
}
}
return -1;
}
int main()
{
int arr[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i=7;
int ret=find_second(arr, 7, 0, 10);
if(ret == -1)
printf("找不到");
else
printf("找到了,下标为%d",ret);
return 0;
}