Use while instead of using recursive in C++ (#19320)

This commit is contained in:
Phan Nguyễn Tuấn Kiệt
2018-10-15 23:46:42 +07:00
committed by Todd Chaffee
parent 1920b0b4fa
commit 619f955faa

View File

@ -181,14 +181,13 @@ int binarySearch(int a[], int l, int r, int x) {
```C++
int binary_search(int arr[], int l, int r, int target)
{
if (r >= l)
while (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
return binary_search(arr, l, mid-1, target);
return binary_search(arr, mid+1, r, target);
if (arr[mid] > target) r = mid - 1;
else l = mid + 1;
}
return -1;
}