Created
March 28, 2020 02:08
-
-
Save joonas-yoon/d379e8a976533024962ab02329c637eb to your computer and use it in GitHub Desktop.
Binary search - why it can be checked by lower bound
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool binary_search(int arr[], int len, const int& key) { | |
int l = 0, r = len - 1; | |
while (l <= r) { | |
int mid = (l + r) / 2; | |
if (arr[mid] < key) | |
l = mid + 1; | |
else | |
r = mid - 1; | |
} | |
return a[l] == key; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment