Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Created January 1, 2015 20:56
Show Gist options
  • Save kidGodzilla/92508419261b98ea6a30 to your computer and use it in GitHub Desktop.
Save kidGodzilla/92508419261b98ea6a30 to your computer and use it in GitHub Desktop.
Fast Binary Search in JavaScript, unminified
function(array, value) {
var j = 0, length = array.length;
// A for loop was used to save space. More easily understood as a while loop. Exits if our pointer moves out of range.
while (j < length) {
var i = (length + j - 1) >> 1; // move the pointer to the median value using a shift in place of Math.floor() (to save characters)
// If the value we're searching for is greater than the median, move our lower-bound past the median
if (value > a[i])
j = i + 1;
// Otherwise, if the value we're searching for is less than the median, move our upper-bound to the median value
else if (value < array[i])
length = i;
// If neither of the above conditions have been met, we have found our value. Congratulations! Return it!
else
return i
}
// If we've somehow exited the loop, our value was not present and we should return "Not Found".
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment