Skip to content

Instantly share code, notes, and snippets.

@viiiprock
Forked from ba0f3/binary-search.js
Last active June 14, 2020 01:37
Show Gist options
  • Select an option

  • Save viiiprock/7c906a60349b4e5ae4d1c3ef43c3aba8 to your computer and use it in GitHub Desktop.

Select an option

Save viiiprock/7c906a60349b4e5ae4d1c3ef43c3aba8 to your computer and use it in GitHub Desktop.
binary search algorithm
function binarySearch(targetValue, array) {
let min = 0;
let max = array.length - 1;
let count = 0;
let guessedIndex;
while (min <= max) {
count++;
guessedIndex = Math.floor((max + min) / 2);
if (array[guessedIndex] === targetValue) {
return `Total count is ${count} times, and result is at ${guessedIndex}`;
} else if (targetValue <= array[guessedIndex]) {
max = guessedIndex - 1;
} else {
min = guessedIndex + 1;
}
}
return `Total count is ${count} times, and result is not found`;
}
const primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
const result = binarySearch(73, primes);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment