Last active
February 20, 2021 20:14
-
-
Save osha7/687ce3975b8cee36acb91456f1f43557 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 arr = [1, 5, 14, 23, 27, 32, 40, 54, 59, 67, 73] | |
let target = 54 | |
let binarySearchFunction = function(arr, target) { | |
let leftPointer = 0 | |
let rightPointer = arr.length - 1 | |
while (leftPointer <= rightPointer) { | |
let med = Math.ceil((rightPointer + leftPointer) / 2) | |
if (arr[med] === target) return med | |
if (arr[med] > target) { | |
rightPointer = med - 1 | |
} | |
if (arr[med] < target) { | |
leftPointer = med + 1 | |
} | |
} | |
return -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment