Last active
July 6, 2018 03:26
-
-
Save laniehei/692bee78e579c16d1e71428af851dd3e 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(list, item) { | |
var min = 0; | |
var max = list.length - 1; | |
var guess; | |
while (min <= max) { | |
guess = Math.floor((min+max)/2); | |
if (list[guess] === item) { | |
return guess; | |
} | |
else { | |
if (list[guess] < item) { | |
min = guess + 1; | |
} | |
else { | |
max = guess - 1 | |
} | |
} | |
} | |
return -1; | |
} | |
binarySearch([2,6,7,90,103], 90); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment