int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 7;//要查找的数字
//在这个arr有序数组中查找k的下标的值
int sz = sizeof(arr) / sizeof(arr[0]);//数组的元素个数
int left = 0;
int right = sz - 1;
while (left<=right)
{
int mid = (left + right) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
printf("找到了,下标为:%d\n", mid);
break;
}
}
return 0;
}
标签:二分,arr,right,int,mid,查找,left
From: https://blog.csdn.net/yuan_wanqiang/article/details/139245169