Created
January 7, 2024 04:06
-
-
Save MarwanShehata/e83d68ac36364466c6a201f756dd1185 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
function binarySearch(list, item) { | |
let min = 0; | |
let max = list.length - 1; | |
let guess; | |
while (min <= max) { | |
guess = Math.floor((min + max) / 2); | |
if (list[guess] === item) { | |
return guess; | |
} else { | |
if (list[guess] < item) { | |
min = guess + 1; | |
} else { | |
max = guess - 1; | |
} | |
} | |
} | |
return -1; | |
} | |
console.log(binarySearch([2, 6, 7, 90, 103], 90)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment