折半查找非递归算法
#include<iostream>
using namespace std;
int BinSearch(int arr[], int n, int target)
{
int l = 0, r = n - 1;
while (l <=r )
{
int mid = l + (r - l) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int res = BinSearch(a, 10, 3);
cout << res << endl;
return 0;
}