int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
int sz = sizeof(arr) / sizeof(arr[0]);//15 number
int k = 9;//what is i can find
int left = 0;
int right = sz - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] > k)
{
right = mid - 1;
}
else if (arr[mid] < k)
{
left = mid + 1;
}
else
{
printf("yes_%d",arr[mid]);
break;
}
}
if (left > right)
printf("no");
return 0;
}
新学的对顺序的简单二分法遍历
于 2023-04-03 21:31:48 首次发布