Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created June 3, 2020 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaveshmunot1/ecadf9077a156fbacf616f0aa43f34bf to your computer and use it in GitHub Desktop.
Save bhaveshmunot1/ecadf9077a156fbacf616f0aa43f34bf to your computer and use it in GitHub Desktop.
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