Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

binarySearch

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 item;

    if (list[guess] < item) {
      min = guess + 1;
    } else {
      max = guess - 1;
    }
  }
  return -1;
}

console.log(binarySearch([2, 6, 7, 90, 108], 90));
 
PREVIOUS NEXT
Tagged: #binarySearch
ADD COMMENT
Topic
Name
3+8 =