lou.tools
EN

Binary search

Binary search, step by step

Watch it find any value in a sorted list, step by step, with its pseudocode and its Python.

Updated

Free, in your browser. Your lists never leave it.Privacy

A sorted list of 8 values. Find 98.

Comparisons: 0low = 0 · mid = — · high = 7

What happens next?

The run stops five times and asks what comes next. Answer, and it plays on.

Pseudocode and implementation

Pseudocode

  1. The list is sorted. The target can only be between low, the first position, and high, the last.
  2. While there is still a value between low and high:
  3. mid is the position in the middle: (low + high) // 2.
  4. If the value at mid is the target, it is found: return mid.
  5. If it is smaller than the target, the target can only be on its right: low becomes mid + 1.
  6. Otherwise it can only be on its left: high becomes mid − 1.
  7. The range is empty: the target is not in the list. Return −1.

Python

def binary_search(a, target):    low, high = 0, len(a) - 1    while low <= high:        mid = (low + high) // 2        if a[mid] == target:            return mid        if a[mid] < target:            low = mid + 1        else:            high = mid - 1    return -1

How it works

Binary search finds a value in a sorted list without looking at every value. It keeps a range, from low to high, where the target can still be; at first it is the whole list. Each round it looks at the value in the middle of the range, at mid. If it is the target, the search is over. If it is smaller, the target can only be on its right, so low moves to mid + 1; if it is greater, high moves to mid − 1.

Every round halves the range, so a list of n values needs at most ⌊log₂ n⌋ + 1 rounds: 4 for 8 values, 5 for 16, 20 for a million. A linear search, one value after another, may need all n.

It only works on a sorted list: that is what lets one comparison rule out half of what is left. If the target is not in the list, the range ends empty, with low past high, and the function returns −1.

Complexity

Best caseO(1)
Average caseO(log n)
Worst caseO(log n)
Extra spaceO(1)

n is how many values the list has.

Questions

When is binary search a good choice?
Whenever the list is sorted and you will search it more than once: a dictionary, an index, a price table. If the list is not sorted and you search it only once, a linear search is simpler, because sorting first costs more than one search.
How is it different from a linear search?
A linear search looks at the values one by one, so it needs up to n comparisons, but it works on any list. Binary search needs a sorted list and, in exchange, about log₂ n comparisons: 20 instead of a million for a million values.
What is the most common mistake?
Moving low or high to mid instead of past it. If low becomes mid rather than mid + 1, a range of two values can repeat forever. The value at mid has already been checked, so the new range leaves it out.
How does "What happens next?" work?
It searches a new random sorted list for a target that takes several rounds, and stops five times to ask what comes next: which position is mid, and whether the target is found or the search goes on to the left or to the right. Your answers stay in your browser.

More algorithms