Skip to content

Instantly share code, notes, and snippets.

@AmaxJ
Created May 27, 2015 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmaxJ/91562e2d5d4ab1e9b1e8 to your computer and use it in GitHub Desktop.
Save AmaxJ/91562e2d5d4ab1e9b1e8 to your computer and use it in GitHub Desktop.
bsearch
var binarySearch = Assessment.binarySearch = function(array, target){
var point = Math.floor(array.length / 2);
if( array[point] === target)
return target;
else if ( target > array[array.length -1] || target < array[0])
return -1;
else if( array[point] > target )
return binarySearch(array.splice(0, point - 1), target);
else if( array[point] < target )
return binarySearch(array.splice(point + 1, array.length - 1), target);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment