Skip to content

Instantly share code, notes, and snippets.

@aackerman
Created June 13, 2012 01:13
Show Gist options
  • Save aackerman/2921177 to your computer and use it in GitHub Desktop.
Save aackerman/2921177 to your computer and use it in GitHub Desktop.
Binary Search
//binary search
var search = function(array, i) {
var min = 0,
max = array.length - 1,
mid = Math.floor((min + max)/ 2);
while (max >= min) {
if(array[mid] < i) {
min = mid + 1;
} else if(array[mid] > i) {
max = mid - 1;
} else {
return mid;
}
mid = Math.floor((min + max)/ 2);
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment