Reading preferences
Concept

Binary Search

Use an already sorted row to rule out half the remaining positions at each decision.

search, binary search, sorted, interval, midpoint, logarithmic

Search an ordered row of cards

Imagine cards already ordered as [1, 3, 5, 7, 9, 11, 13]. You want 11. Instead of starting at the left, check the middle card, 7.

11 is larger than 7. Because the cards are ordered, none of the cards at or to the left of 7 can be 11. Now search only the cards on the right. This repeated narrowing is Binary Search.

Why order lets us skip work

In Linear Search we inspect items one by one. Here a comparison tells us about many items at once because their order is known.

The search interval is the range of positions still possible. left and right mark its first and last indices. These bounds include both endpoints. A midpoint is an index near the centre of that interval.

Where it is useful and what it assumes

Binary Search can help locate an item in a sorted array or Python list, or reason about a threshold in an ordered sequence. A familiar analogy is opening an alphabetically ordered directory near its middle and choosing which half to inspect.

This example assumes ascending sorted values with consistent comparisons and efficient index access. It does not sort the input. It can give a wrong answer on an unsorted list, even if it happens to work on one example. It also does not automatically apply efficiently to a linked list.

Watch the possible region shrink

Find target 11:

Left indexRight indexMiddle indexMiddle valueDecision
0637Target is larger; keep indices 4 to 6
46511Match; return index 5

For missing target 8, the same first step keeps indices 4 to 6. Then 11 is too large, leaving only index 4, whose value is 9. That is also too large, so no positions remain and we return no result.

See the reasoning

Try Binary Search one decision at a time

Find the target by checking the numbered cards. Index 0 is the first position. This is a teaching demonstration, not live Python execution.

The cards are already sorted from smallest to largest. A match can be any matching index.

Target: 11

  1. 1Index 0Not ruled out
  2. 3Index 1Not ruled out
  3. 5Index 2Not ruled out
  4. 7Index 3Not ruled out
  5. 9Index 4Not ruled out
  6. 11Index 5Not ruled out
  7. 13Index 6Not ruled out

Start

The cards are already sorted. We will check the middle of the possible region.

Why discarding a half is safe

If the middle value is smaller than the target, every value at or left of it is also too small. If the middle is larger, every value at or right of it is too large. Therefore, if a match exists, it stays inside the remaining interval.

Each failed comparison excludes the tested middle. The interval gets smaller and eventually becomes empty or contains a match. This explains correctness and termination rather than relying on a lucky example.

Python illustration

def binary_search(values, target):
    left = 0
    right = len(values) - 1
    while left <= right:
        middle = (left + right) // 2
        if values[middle] == target:
            return middle
        if values[middle] < target:
            left = middle + 1
        else:
            right = middle - 1
    return None

values = [1, 3, 5, 7, 9, 11, 13]
print(binary_search(values, 11))
print(binary_search(values, 8))

Prepared output — not live execution:

5
None

// 2 rounds down to an integer index. left <= right permits checking a single remaining slot. The + 1 and - 1 exclude the middle already checked. An empty input starts with right = -1, so the loop does not read any item.

Time and storage trade-offs

Halving the remaining region gives O(log n) worst-case time for already sorted random-access data under constant-cost comparisons. An immediate middle match is O(1) best-case time. This iterative version uses O(1) extra storage.

Sorting beforehand has its own cost and may change the original indices. For one search on an unsorted list, Linear Search can be a better choice. Do not describe sorting plus searching as simply O(log n).

Duplicates and common mistakes

This function returns a matching index, not necessarily the first when there are duplicates. Index 0 is valid; None signals no match.

Keeping left = middle after a failure can repeat the same position and prevent termination. Updating bounds must match the inclusive interval rule used here. Other correct Binary Search variants use different boundary rules; do not mix them without rechecking the reasoning.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What must be true for this Binary Search to work?
2. The middle value is 8 and the target is 3. Which part can be ruled out?
3. If a target appears more than once, what does our implementation promise?
4. Why do we move left to middle + 1 or right to middle - 1 after a failed match?

References and further reading