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.
Search target
Live controls
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.
Current probe
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 -1Execution trace
27 is smaller than 42, so discard the left half.
56 is larger than 42, so discard the right half.
31 is smaller than 42, so discard the left half.
42 matches the target. Search complete.