-
-
Save eengineergz/eb8d1e1684db15cc2c8af28e13f38751 to your computer and use it in GitHub Desktop.
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
function binarySearch(array, target) { | |
let start = 0; | |
let end = array.length - 1; | |
while (start <= end) { | |
let midpoint = Math.floor((start + end) / 2); | |
if (target === array[midpoint]) { | |
return midpoint; | |
} | |
if (target > array[midpoint]) { | |
start = midpoint + 1; | |
} | |
if (target < array[midpoint]) { | |
end = midpoint - 1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment