Created
September 8, 2022 18:45
-
-
Save soe/8c17e633e2e14b2a2ca7acb81e867101 to your computer and use it in GitHub Desktop.
binarySearch pseudocode recursion method
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
binarySearch(array, target, startIndex, endIndex) | |
// target might still be in the range | |
if(endIndex >= startIndex) | |
midIndex = (startIndex + endIndex) // 2 | |
// found! | |
if(target == array[midIndex]) | |
return midIndex | |
// target is in the higher right side | |
else if(target > array[midIndex]) | |
return binarySearch(array, target, midIndex + 1, endIndex) | |
// target is in the lower left side | |
else | |
return binarySearch(array, target, startIndex, midIndex - 1) | |
// target in not in the range | |
else | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment