Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implement Binary Search using recursion.
bool binary_search(vector<int> sorted_nums, int low, int high, int K) {
if (low > high) {
return false;
}
int mid = (low+high)/2;
if (sorted_nums[mid] == K) {
return true;
}
bool is_found = false;
if (sorted_nums[mid] < K) {
is_found = binary_search(sorted_nums, mid + 1, high, K);
} else {
is_found = binary_search(sorted_nums, low, mid - 1, K);
}
return is_found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.