Created
June 13, 2012 01:13
-
-
Save aackerman/2921177 to your computer and use it in GitHub Desktop.
Binary Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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