Skip to content

Instantly share code, notes, and snippets.

@soe
Created September 8, 2022 18:45
Show Gist options
  • Save soe/8c17e633e2e14b2a2ca7acb81e867101 to your computer and use it in GitHub Desktop.
Save soe/8c17e633e2e14b2a2ca7acb81e867101 to your computer and use it in GitHub Desktop.
binarySearch pseudocode recursion method
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