#include<iostream>
using namespace std;
void BinarySearch(int a[],int n)
{
int target;
cout << "输入要查找的值" << endl;
cin >> target;
int low=0, high=n-1;//位于要查找区间的最低和最高位
int mid;//高位和低位的中值
int index = -1;//要查找点的位置
while (low <= high)
{
mid = (low + high) / 2;
if (a[mid] == target)
{
index = mid;
break;
}
else if (a[mid] > target) high = mid - 1;
else low = mid + 1;
}
if (index == -1)cout << "找不到" << endl;
else cout << target << " 位置在 " << index << endl;;
}
int main()
{
int a[10] = { 2,3 ,7,9,10,12,14,15,17,33 };
cout << "从0开始" << endl;
BinarySearch(a, 10);
}
10-06
1328