Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 543 Bytes

Algorithms.md

File metadata and controls

24 lines (21 loc) · 543 Bytes

Algorithms

Binary Search

function binarySearch(arr, target) {
  let low = 0; 
  let high = arr.length; 
  while (low < high) {
    const middleIndex = Math.floor(low + (high - low) / 2); 
    if (arr[middleIndex] === target) {
      return middleIndex; 
    } else if (arr[middleIndex] > target) { // look left
      high = middleIndex; 
    } else { // look right
      low = middleIndex + 1; 
    }
  }
  return -1; 
}

const result = binarySearch([1, 2, 3, 4, 5], 3); 
console.log(result); // index 2 is returned