#include<stdio.h>
int binary_search(int arr[], int x, int sz)
{
int left = 0;
int right = sz - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] < x)
left = mid + 1;
else if (arr[mid] > x)
right = mid - 1;
else
return mid;
}
return -1;
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9 };
int x;
scanf("%d", &x);
int sz = sizeof(arr) / sizeof(arr[0]);
int ret = binary_search(arr, x, sz);//括号里放的是arr,不是arr[10]!!!
if (ret == -1)
{
printf("找不到了");
}
else
{
printf("找到了,下标是%d", ret);
}
return 0;
}