enh(CPlusPlus): update comment of binary search (#807)

pull/811/head
Mohnish Deshpande 2022-08-21 01:50:26 +10:00 committed by GitHub
parent d34acc78b0
commit f4353ff2a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -7,7 +7,7 @@ int binarySearch(int arr[], int l, int r, int x)
{ {
if (r >= l) { if (r >= l) {
int mid = l + (r - l) / 2; int mid = l + (r - l) / 2;
//We use (l + (r - l)) rather than using (l - r) to avoid arithmetic overflow. //We use (l + (r - l)) rather than using (l + r) to avoid arithmetic overflow.
//Arithmetic overflow is the situation when the value of a variable increases //Arithmetic overflow is the situation when the value of a variable increases
//beyond the maximum value of the memory location, and wraps around. //beyond the maximum value of the memory location, and wraps around.
@ -15,7 +15,7 @@ int binarySearch(int arr[], int l, int r, int x)
if (arr[mid] == x) if (arr[mid] == x)
return mid; return mid;
// If element is smaller than mid, then it can only be present in left subarray // If mid is greater than element, then it can only be present in left subarray
if (arr[mid] > x) if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, l, mid - 1, x);