Last active
April 25, 2019 18:28
-
-
Save kevinah95/1557bd9c3c63a949edcdf5b9b066608d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = (arr, left, right, searchValue) => { | |
| if (left <= right) { | |
| middle = Math.floor((left + (right - left) / 2)) | |
| if (searchValue === arr[middle]) { | |
| return middle | |
| } | |
| if (searchValue < arr[middle]) { | |
| return binarySearch(arr, left, middle - 1, searchValue) | |
| } else { | |
| return binarySearch(arr, middle + 1, right, searchValue) | |
| } | |
| } | |
| return -1 | |
| } | |
| let arr = [1, 2, 3, 4, 9] | |
| console.log("Index of 9 element = "+binarySearch(arr, 0, arr.length - 1, 9)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment