Last active
October 10, 2017 13:50
-
-
Save jmlavoier/166e1c2304496c06ece7264229a79f1d to your computer and use it in GitHub Desktop.
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
let binarySearch = function (array, valueSearch) { | |
let start = 0; | |
let end = array.length - 1; | |
return function search(start, end) { | |
let mid = Math.round((start + end) / 2); | |
let length = end - start; | |
if (length < 0) return -1; | |
if (array[mid] === valueSearch) { | |
return mid; | |
} else if (array[mid] > valueSearch) { | |
return search(start, mid - 1); | |
} else if (array[mid] < valueSearch) { | |
return search(mid + 1, end); | |
} | |
}(start, end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment