Skip to content

Instantly share code, notes, and snippets.

@dfparker2002
Forked from mrsweaters/binary_search.js
Created September 6, 2016 09:46
Show Gist options
  • Save dfparker2002/7aa558ea12fa0a3da8907f537a450c12 to your computer and use it in GitHub Desktop.
Save dfparker2002/7aa558ea12fa0a3da8907f537a450c12 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