All visualizationsDSA Patterns · 72 of 76
🧪 Your experiment — type any numbers, pick a target (present or missing!), and hunt for it
6 numbers (1–99). We will SORT them first — binary search refuses to work on unsorted data, and that rule is half the lesson.
nums =find:
Binary Search Lab — hunt for YOUR number
Type six numbers and a target — then watch half the possibilities die with every single comparison.
💡
THE BIG IDEA
Binary search is the interview classic: instead of checking boxes one by one, look at the middle, throw away the half that cannot contain your target, and repeat. Type your own numbers and pick your own target — including one that is NOT there, because watching low and high squeeze shut on nothing is the part everyone fumbles in interviews. One rule first: we sort your numbers before starting. Binary search demands it.
Move your mouse over any line to see what it does. Hover a loop line and it plays through every repeat. Or press ▶ Play to watch the whole thing. On a phone, tap the arrows.
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
low:int
0
high:int
5
found:int
-1
🖥️ What the computer shows
the answers the program prints out
nothing yet
1/11
UP NEXT IN DSA PATTERNS
Coin Change Lab — race the table against greedy
Pick your own coins and amount, watch the DP ladder fill — and catch grab-the-biggest paying more than it had to.
Trie Autocomplete — Words That Share a StartCoin Change Lab — race the table against greedy