1nums = [4, 9, 15, 23, 38, 57]
2target = 15
3low, high, found = 0, 5, -1
4while low <= high and found == -1:
5 mid = (low + high) // 2
6 if nums[mid] == target:
7 found = mid
8 elif nums[mid] < target:
9 low = mid + 1
10 else:
11 high = mid - 1
12print("found at index", found)
●We are hunting for 15 among your numbers — without checking them one by one.
Binary search never scans. It looks at the MIDDLE, throws away the half that cannot contain the target, and repeats. Watch how few looks it needs.
📦 Memory boxes
what the program is remembering right now
🖥️ What the computer shows
the answers the program prints out
nothing yet ▌