Skip to content

Instantly share code, notes, and snippets.

@chrisdc
Last active October 11, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdc/6268d0b6624291a93d56fd98c1698a1b to your computer and use it in GitHub Desktop.
Save chrisdc/6268d0b6624291a93d56fd98c1698a1b to your computer and use it in GitHub Desktop.
Binary Search
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