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.
What happens next?
The run stops five times and asks what comes next. Answer, and it plays on.
Pseudocode and implementation
Pseudocode
- The list is sorted. The target can only be between low, the first position, and high, the last.
- While there is still a value between low and high:
- mid is the position in the middle: (low + high) // 2.
- If the value at mid is the target, it is found: return mid.
- If it is smaller than the target, the target can only be on its right: low becomes mid + 1.
- Otherwise it can only be on its left: high becomes mid − 1.
- 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 -1How 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 case | O(1) |
|---|---|
| Average case | O(log n) |
| Worst case | O(log n) |
| Extra space | O(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
Bubble sort
LiveWatch neighbours swap until the list is sorted, with pseudocode, Python and a quiz.Selection sort
LiveWatch each pass pick the smallest value and swap it to the front, with pseudocode, Python and a quiz.Insertion sort
LiveWatch each value slide left into the part already in order, with pseudocode, Python and a quiz.Merge sort
LiveWatch the list split into halves and merge back sorted, with pseudocode, Python and a quiz.Quicksort
LiveWatch each pivot split the list and land in its final place, with pseudocode, Python and a quiz.AI text cleaner
LiveText from ChatGPT, Claude or Gemini without hidden characters, citation leftovers or broken spacing.