Home Binary search Skip to playground

Binary search: why halving beats scanning

Short lesson below, then interactive pieces you can run. Same idea everywhere: sorted data, compare the middle, throw away half the search space.

The Problem vs. the Solution

Most people explain why software feels fast by pointing at hardware: faster processors, more RAM, better networks. Those ingredients matter, but they are not the whole story—and they are not what makes huge problems feel small.

When you search the web, page through a large dataset, or query a big system, the underlying data can be enormous. If the program had to touch every record, every time, the math would break long before the silicon got “fast enough.” Useful answers still arrive quickly because the system avoids doing unnecessary work.

The real advantage is not raw speed alone; it is shrinking how much work has to happen—the algorithms and structures that skip most of the haystack instead of walking it end to end.

Playground

Try binary search on your own numbers

Pick binary or linear, edit the list and target, then Prepare steps. Use Step or Play / Pause.

low · left bound probe · middle high · right bound
500ms
Press Prepare steps, then Step or Play.
0/0
Comparisons: 0
Index:
Quick guide
  1. Choose Binary (sorted list) or Linear (any order).
  2. Edit the list and target, or use Random list.
  3. Press Prepare steps. Then Step or Play / Pause.

If you change the list, target, or mode, press Prepare steps again.

Array

Swipe sideways if the row is wider than the screen.

This step
Pseudocode (live line)
Active line

Why this matters

Picture a phone book with one million names in random order. Someone asks you to find Rahul Sharma.

Where do you start?

Interactive Checkpoint
Choose one option
How would you search first — choose one option

Select an option and click "Check answer".

The Problem With Linear Search

Let us look at worst-case checks:

  • 100 names -> 100 checks
  • 10,000 names -> 10,000 checks
  • 1,000,000 names -> 1,000,000 checks

If data becomes 10x bigger, what happens?

Interactive Checkpoint
Choose one option
If data becomes 10x bigger — choose one option

Select an option and click "Check answer".

Key note

What does O(n) mean?

In computer science, Big O notation describes how the amount of work grows as the input size grows. The letter O is read as “order of”; n is the size of your data (for example, number of names in the phonebook). So O(n) means: in the worst case, work grows in proportion to n.

That is why linear search is called linear growth: we write it as O(n) (no special symbols needed in normal text).

Visualization
Linear search and O(n)

Bar height shows worst-case comparisons: for linear search, that equals n. When n grows, the bar grows in the same proportion.

step 1/4

What if the phonebook is in alphabetical order?

Keep the same million names, but list them from A to Z, like a printed phonebook. Once the names stay in order, you do not need to start at page one every time: you can open near the middle, compare one name, and tell which half of the book you can skip next.

Interactive Checkpoint
Choose one option

If you jump to the middle of a sorted phonebook, what should you do next?

Interactive checkpoint: middle of sorted phonebook

Select an option and click "Check answer".

The Core Idea of Binary Search

Binary search is a short loop you run on sorted data: look at the middle of the range that could still contain the answer, compare it to the value you want, and use the ordering to decide which side of the list can be ruled out. Then do the same thing again on the smaller range, until you find the value or nothing is left to search.

You are not walking the list from front to back; each comparison removes a large slice of remaining options. The gain comes from fewer steps overall, not from buying a faster chip so each step feels a little quicker.

Visualization
Check the middle → remove half → repeat

Fifteen sorted slots (values 1 … 15). Target value 10. Active range is kept; the rest fades out after each decision.

step 1/3

Key note

What is a logarithm, and how do you read log₂(128)?

A logarithm answers: “To what exponent must I raise the base, to get this number?” The little 2 in log₂ is the base (here, powers of two).

In symbols: log₂(128) = x means the same thing as 2x = 128. So you are looking for the exponent x that turns 2 into 128.

Worked example

Solve log₂(128). The walkthrough starts on its own; use Pause if you want to read a step, then Play to continue. It loops when finished.

Visualization
From “log” wording to the number 7

Each ×2 along the chain is one doubling. Seven doublings turn 1 into 128, so the exponent is 7.

Doublings: —
1 / 10
1 Same problem, exponent form
2 Count doublings: start at 1, multiply by 2 until you reach 128
Progress through the story 0%

Binary search keeps halving the remaining range (divide by two, over and over). The number of halvings you need, for a list of size n, is in the same family as log₂(n)—that is why people call it logarithmic and write O(log n) for the growth rate.

That is why binary search is called logarithmic growth: you only need about as many middle checks as you can halve the list down to one candidate, which scales like log₂(n). In Big O we write that as O(log n)—same meaning as the logarithm above, without needing special notation in normal text.

Work Reduction Animation
Same target, two search methods: linear checks one-by-one, binary jumps to the middle and keeps halving the range.
Same toy setup: linear may need 12 checks (~0.96s), binary can finish in 3 probes (~0.24s). Same speed device — different method.
stage 1/4
Target to find: Doc 12
Linear Search (one by one)
0 checks
Binary Search (sorted docs)
0 probes
Linear starts at Doc 1. Binary starts at the middle (Doc 10).
linear vs binary

Halving the search space

Now here is the most important idea:

Binary search works like this:

Every step divides the problem into half.

Interactive Checkpoint
Choose one option

If you have 32 items: how many times can you divide by 2 until 1 remains?

Try the chain

32 16 8 4 2 1
Halving 32 items until 1 remains

Select an option and click “Check answer”.

Interactive Checkpoint
Choose one option

Now think: what if we double the list to 64 items? How many divide-by-two steps until 1 remains?

Hint: 64 → 32 → 16 → 8 → 4 → 2 → 1 — count the arrows (each arrow is one halving).

Halving 64 items until 1 remains

Select an option and click “Check answer”.

Why logarithmic search wins

Let’s compare at real-world sizes:

Rough step counts: linear versus binary search for one million items
Method 1,000,000 items
Linear search up to 1,000,000 steps
Binary search ~20 steps

About 20 steps matches log₂(1,000,000) rounded up. Exact counts vary a little by code, but binary search stays tiny next to linear.

Longer comparison table — from 100 items to 1 billion.

Interactive Checkpoint
Choose one option

If the data becomes 1 billion items, how many binary-search steps (same “halve the range” idea)?

Binary search steps for one billion items

Select an option and click “Check answer”.

After you check: the work does not explode when the list gets huge — it grows slowly, like a logarithm.

The Geometry of the Middle

Binary search works only when the data is sorted, because the order is what makes each step a reliable rule for which side of the list can still contain the answer.

Once sorted, the list becomes a decision space: you compare the target against the middle element of the current range, and that single comparison rules out about half of the remaining data, so each probe divides the work still ahead by two.

Hands-on: compare to the middle, then drop half the list

Read the story on the left, then practice on the right. The playground at the top uses the same low, high, and middle index.

How binary search feels (no code yet)

Picture a row of numbers in increasing order. You are looking for 23—not by scanning every cell, but by asking a few smart questions.

2, 5, 8, 12, 16, 23, 38, 56, 72

Step 1 — Start with the full row

You do not yet know where 23 sits. So you look at the middle of the part you still trust. In the full list, the middle value is 16.

Step 2 — Compare

Ask: is 23 smaller than, equal to, or larger than 16? It is larger. Because the list is sorted, everything to the left of that middle is too small to be 23—you can mentally drop that whole left block.

Step 3 — Repeat on what is left

What remains is the right side: 23, 38, 56, 72. You take the middle again. This time it lands on 23—so you are done.

Key idea

Every time: peek at the middle of the current window, compare to the target, and throw away about half of what is still in play. That is the same rhythm the algorithm follows.

Important rule

This only works when values are sorted along the row. If the same numbers were placed in random slots, you could not trust “drop the left half” after one comparison—use See the problem: jumbled list in the panel to watch that failure mode.

Check your understanding

Use the nine cells below. Choose Practice: sorted list and press Begin—you will answer twice, and the gray cells show what got ruled out. Then switch to See the problem: jumbled list for the same numbers in a random order.

Value we are searching for: 23

Still could contain the answer Middle (compare to 23) Ruled out this round
Nine slots (index printed under each value)

Pick Practice: sorted list or See the problem: jumbled list, then press Begin.

Quick check
Choose one option

Why does binary search insist on sorted data?

Why sorted data is required for binary search

Select an option and click “Check answer”.

Linear search vs the binary search algorithm

Same job—find a value—but different rules and different growth as data gets large.

Linear search

  • Works on any list—sorted or unsorted.
  • In the worst case, visits every element.
  • Time complexity: O(n)

Binary search algorithm

  • Requires a sorted list (ascending or descending, consistently).
  • Each step cuts the remaining search range roughly in half.
  • Time complexity: O(log n) — doubling data adds about one more step.

How the binary search algorithm works

Keep a window of indices [low, high], always look at the middle of that window, then shrink the window.

  1. 1 Set two pointers: low (start) and high (end).
  2. 2 Compute middle index: mid = low + (high - low) / 2.
  3. 3 Compare a[mid] with the target.
  4. 4 If equal, return mid.
  5. 5 If target is larger, move right: low = mid + 1.
  6. 6 If target is smaller, move left: high = mid - 1.
  7. 7 Repeat until found, or until low > high (not in the array).

Same example as the hands-on lab (with indices)

Middle index uses mid = low + (high - low) / 2 with whole-number division.

Array

[2, 5, 8, 12, 16, 23, 38, 56, 72]

Target

23

1

Window indices 0–8 → middle index 4, value 16

23 is greater than 16, so discard the left side; continue with indices 5–8.

2

Window indices 5–8 → middle index 6, value 23

Equal to the target → found at index 6.

2 comparisons (two middle probes) for this target—not scanning the whole array.
Reference implementations

The code behind it

Same algorithm everywhere: two bounds, one middle index, compare, shrink the window. Pick a language—behavior matches the interactive panel at the top of this page.

Language

Java
public static int binarySearch(int[] arr, int target) {
    int low = 0, high = arr.length - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        if (arr[mid] == target) return mid;
        else if (target > arr[mid]) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}

Simple code. Massive impact.

Why the binary search algorithm feels fast

Linear work grows in proportion to how much data you have. The binary search algorithm grows with how many times you can cut the remaining range in half—that is a logarithm, so the step count stays small even when N explodes.

Try it

Move the slider: pretend the list has this many items

100 items

100 10k 1M 1B

Linear search (worst case)

100 comparisons (worst case, scan the whole list)

Binary search algorithm

~7 middle probes (halve the range each time)

More sizes (same comparison)

These numbers are rough worst-case steps, good for intuition.

Rough comparison of linear versus binary search steps at increasing data sizes
Data size Linear (worst) Binary (~halvings)
100100 steps~7 steps
10,00010,000 steps~14 steps
1,000,0001,000,000 steps~20 steps
1,000,000,0001,000,000,000 steps~30 steps

Check your read · linear

You double the dataset from N to 2N. Linear search worst case roughly…

Linear search when data doubles

Choose an option, then Check answer.

Check your read · binary

You double the dataset from N to 2N. Binary-search-style probes roughly…

Binary search when data doubles

Choose an option, then Check answer.

Fast intuition — tap to expand

Each card is one sentence you can reuse when explaining performance.

Million-item lists

About 20 binary-style probes versus up to a million linear checks in the worst case.

Billion-item scale

Roughly 30 halvings versus up to a billion one-by-one comparisons.

Doubling the data

When N doubles, linear worst-case work about doubles; the binary search algorithm usually needs only about one more comparison round.

Where this idea shows up

Same pattern: sorted or indexed data + discard half the remaining space of possibilities.

Databases

Indexes and storage engines keep keys ordered so lookups can jump in logarithmic time instead of scanning every row.

Networks

Routing tables and search structures often rely on ordering so systems can narrow where a packet or rule might live.

Apps & platforms

Autocomplete, sorted feeds, and “jump to” features reuse the same halving logic under the hood.

Developer tools

Version control, bisecting bugs, and searching sorted logs all benefit from cutting the candidate set in half.

The trade-off (teach this as a story)

Up-front cost

The binary search algorithm only “trusts” halves if data is sorted (or indexed so you can treat it like sorted). Sorting or building an index costs time and space before the first search.

What you buy

After that prep, each lookup stays cheap. In practice systems often pay once to sort or index, then answer millions of queries with tiny probe counts.

The algorithmic mindset

Binary search is a small loop, but it trains a bigger habit.

Avoid

Checking everything “just to be sure.”

Prefer

Eliminating impossible work as early as the rules allow.

Final thought

Modern systems feel fast when algorithms refuse to do unnecessary work.

Same hardware. Better method. Better result.