Created
October 1, 2020 22:05
-
-
Save manudevcode/ed1846760fb7dfafbddc0afa241bd040 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(value, list) { | |
let first = 0; | |
let last = list.length - 1; | |
let position = -1; | |
let found = false; | |
let middle; | |
while (found === false && first <= last) { | |
middle = Math.floor((first + last)/2); | |
if (list[middle] == value) { | |
found = true; | |
position = middle; | |
} else if (list[middle] > value) { | |
last = middle - 1; | |
} else { | |
first = middle + 1; | |
} | |
} | |
return position; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment