Created
June 3, 2020 09:04
-
-
Save bhaveshmunot1/ecadf9077a156fbacf616f0aa43f34bf to your computer and use it in GitHub Desktop.
Implement Binary Search using recursion.
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(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