All algorithms

Binary Search

Search a sorted array in O(log n) by repeatedly halving the search range — compare the target with the middle element and discard the half it can't be in.

time O(log n)
space O(1)

Press Tab out of the box or click Resetto regenerate frames from the current input.

Visualization
No frames yet — edit input and click Run.
Pseudocode
lo, hi = 0, n-1
while lo <= hi:
  mid = (lo + hi) / 2
  if a[mid] == target: return mid
  elif a[mid] < target: lo = mid + 1
  else:                 hi = mid - 1
return -1