This commit is contained in:
Akash Solanki 2024-07-17 10:38:38 +09:00 committed by GitHub
commit 71e4ab46d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,27 @@
var myList = [1, 3, 5, 7, 9];
function binarySearch(myList, itemToFind) {
var low = 0,
high = myList.length - 1,
mid,
guessed;
while(low <= high) {
mid = Math.floor((low + high) / 2),
guessed = myList[mid];
if(guessed === itemToFind) {
console.log('Found item at index: ' + mid + ', value is ' + guessed);
return;
}
if(itemToFind < guessed) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
binarySearch(myList, 3);