Created
April 6, 2021 00:42
-
-
Save achepukov/7cf291ae32027f5c62b383fff86d9d0b to your computer and use it in GitHub Desktop.
Binary search in javascript
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
const binSearch = (list, val) => { | |
let min = 0, max = list.length - 1, index, guess; | |
while(min <= max) { | |
index = Math.ceil((min + max) / 2, 10); | |
guess = list[index]; | |
if (guess === val) { | |
return index; | |
} else if (guess > val) { | |
max = index - 1; | |
} else { | |
min = index + 1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment