Skip to content

Instantly share code, notes, and snippets.

@Joe1220
Created September 1, 2018 17:23
Show Gist options
  • Select an option

  • Save Joe1220/c14f0706367b9faaeda963ada1db56fa to your computer and use it in GitHub Desktop.

Select an option

Save Joe1220/c14f0706367b9faaeda963ada1db56fa to your computer and use it in GitHub Desktop.
var binarySearch = function (array, target) {
var guess, min = 0, max = array.length - 1;
while(min <= max) {
guess = Math.floor((min + max) / 2);
if(array[guess] === target) {
return guess;
} else if(array[guess] < target) {
min = guess + 1;
} else {
max = guess - 1;
}
}
return -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment