Skip to content

Instantly share code, notes, and snippets.

@the-mgi
Last active November 12, 2020 00:06
Show Gist options
  • Save the-mgi/893fa516cf3ddecf9269b476fe8eef18 to your computer and use it in GitHub Desktop.
Save the-mgi/893fa516cf3ddecf9269b476fe8eef18 to your computer and use it in GitHub Desktop.
function BinarySearchRecursive(array: number[], key, startIndex: number, lastIndex: number): number {
const mid = Math.floor((startIndex + lastIndex) / 2);
if (!(startIndex <= lastIndex)) {
return -1;
} else if (key === array[mid]) {
return mid;
} else if (key < array[mid]) {
BinarySearchRecursive(array, key, startIndex, mid - 1);
} else {
BinarySearchRecursive(array, key, mid + 1, lastIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment