Created
May 27, 2015 03:00
-
-
Save AmaxJ/91562e2d5d4ab1e9b1e8 to your computer and use it in GitHub Desktop.
bsearch
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
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