//折半查找
//author:https://blog.csdn.net/qq_31828515/article/details/51791833
//date:2020.4.23
#include <cstdio>
using namespace std;
int binary_research(int arr[],int left,int right,int element)
{
while(left<=right)
{
int mid = (left+right)/2;
if(arr[mid]>element)
{
right = mid - 1;
}
else if(arr[mid]<element)
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9};
int right = 8;
int left = 0;//定义右下标并初始化为0
int element = 0;//定义查找的元素并初始化为0
printf("请输入要查找的元素:");
scanf("%d",&element);
int location = binary_research(array,left,right,element);
if(location>=0)
printf("The element's location is %d
",location);
else
printf("Don't exit this element.
");
return 0;
}