int View::binarySearchRecursively(int arr[] , int low , int high , int nKey)
{
if(low <= high)
{
int mid = (low + high)/2;
if(arr[mid] == nKey)
{
return mid;
}else if(arr[mid] < nKey)
{
return binarySearchRecursively(arr,mid+1,high,nKey);
}else if(arr[mid] > nKey)
{
return binarySearchRecursively(arr,low,mid-1,nKey);
}
}
}
int View::binarySearch(int arr[], int low, int high, int nKey)
{
/*临时标记值*/
int mid = 0;
while (low <= high) {
mid = (low + high)/2;
if(arr[mid] == nKey)
{
break;
}else if(arr[mid] < nKey)
{
low = mid + 1;
}else{
high = mid - 1;
}
}
return mid;
}