#include <iostream>
using namespace std;
int binarySearch(int arr[],int low,int high,int key){
if(low > high){
return -1;
}
int mid = low + ((high - low)>>1);
int midVal = arr[mid];
if(midVal > key){
binarySearch(arr,low,mid-1,key);
}else if(midVal < key){
binarySearch(arr,mid+1,high,key);
}else{
return mid;
}
}
int main() {
int a[] = {1,11,23,34,45,56,78,90,99};
int res = binarySearch(a, 0, (sizeof(a) / sizeof(a[0]))-1, 23);
cout<<"res = "<<res<<endl;
return 0;
}
标签:二分,arr,int,mid,binarySearch,查找,low,key
From: https://www.cnblogs.com/poteitoutou/p/17090584.html