Skip to content

Instantly share code, notes, and snippets.

@mrsweaters
Created August 9, 2016 02:16
Show Gist options
  • Save mrsweaters/ca9f22cc3d190f5c94e41aec76bb8a95 to your computer and use it in GitHub Desktop.
Save mrsweaters/ca9f22cc3d190f5c94e41aec76bb8a95 to your computer and use it in GitHub Desktop.
Binary Search
var binary_search = function (list, item) {
var low = 0;
var high = list.length - 1;
while(low <= high) {
var mid = Math.floor((low + high) / 2);
var guess = list[mid];
if (guess === item) {
return mid;
} else if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment