Algorithms VisualizedInteractive lesson

Binary search that shows its work.

A visual walkthrough inspired by Algorithm Visualizer: start with a sorted list, probe the midpoint, discard half the search space, and watch the invariants tighten after every step.

Time
O(log n)
Space
O(1)
Focus
Search invariants

Search target

Live controls

step 1/4

Invariant

If the target exists, it must remain inside the inclusive window [low, high]. Every comparison removes half of the remaining candidates.

Visualization

Search window

Orange marks the midpoint probe. Teal marks the active bounds. Faded cells are already eliminated from consideration.

index 0
3
low
index 1
7
index 2
12
index 3
18
index 4
21
index 5
27
mid
index 6
31
index 7
42
index 8
56
index 9
63
index 10
75
index 11
88
high

Current probe

low
0
mid
5
high
11

Narration

27 is smaller than 42, so discard the left half.

Worst case stays at O(log n), even when the target is absent.

Pseudocode

while low <= high:
  mid = floor((low + high) / 2)

  if arr[mid] == target:
    return mid

  if arr[mid] < target:
    low = mid + 1
  else:
    high = mid - 1

return -1

Execution trace

Step 1[0, 11]

27 is smaller than 42, so discard the left half.

Step 2[6, 11]

56 is larger than 42, so discard the right half.

Step 3[6, 7]

31 is smaller than 42, so discard the left half.

Step 4[7, 7]

42 matches the target. Search complete.