Last active
October 11, 2018 10:05
-
-
Save chrisdc/6268d0b6624291a93d56fd98c1698a1b to your computer and use it in GitHub Desktop.
Binary Search
This file contains hidden or 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 search(key, data) { | |
var min = 0; | |
var max = data.length - 1; | |
var half; | |
while (max >= min) { | |
half = Math.floor((max + min)/2); | |
if (data[half] === key) { | |
return half; | |
} else if (data[half] < key) { | |
min = half + 1; | |
} else if (data[half] > key) { | |
max = half - 1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment