← Back

Implement binary search algorithm.

Given an array of integers, do binary search on it.

Algorithm

  1. Binary Search (array must be sorted):
    • Set low = 0 and high = array length - 1.
    • While low ≤ high:
      • Calculate mid = Math.floor((low + high) / 2).
      • If array[mid] equals target, return mid or indicate success.
      • If array[mid] < target, set low = mid + 1.
      • If array[mid] > target, set high = mid - 1.
    • If not found, indicate failure.

Time Complexity

Enter numbers separated by commas