Search
 
SCRIPT & CODE EXAMPLE
 

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));
Comment

PREVIOUS NEXT
Code Example
Javascript :: ArduinoJson.h 
Javascript :: Javascript get / print current path 
Javascript :: how to remove elements from array 
Javascript :: particle js 
Javascript :: split javascript 
Javascript :: some in js 
Javascript :: json html 
Javascript :: js quote 
Javascript :: why we use react js 
Javascript :: modules.exports javascript 
Javascript :: Set Default Parameter Value 
Javascript :: resize window javascript 
Javascript :: javascript date timezone 
Javascript :: object constructor js 
Javascript :: d3 js 
Javascript :: javascript get width of image 
Javascript :: dependency list useeffect 
Javascript :: javascript strings 
Javascript :: Prerequisites before creating react-app 
Javascript :: how to use axios filter 
Javascript :: convert json data into html table 
Javascript :: mongoose query object 
Javascript :: react native firebase 
Javascript :: how reducer works in redux 
Javascript :: lettre au hasard javascript 
Javascript :: npm windows registry 
Javascript :: fetch api example 
Javascript :: Unexpected token < in JSON at position 0 
Javascript :: react native better camera 
Javascript :: date and month are swapping in angular 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =