Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Binary search

Бинарный поиск

This algorithm finds a value in sorted data by checking the middle item and ruling out half of the remaining candidates at each step.

Also known as: двоичный поиск

What binary search does

Suppose you have a sorted list of event timestamps and want to know whether it contains 10:15. If the middle timestamp is 10:30, you can rule out that entry and every entry after it. Check the middle of what's left and repeat. Sorting is what makes this safe: the comparison tells you not just that one entry is wrong, but that a whole section cannot contain the answer.

How the search range shrinks

One common convention is a half-open range [lo, hi): lo is included and hi is excluded. Start with lo = 0 and hi = len(items). If the middle value is smaller than the target, move lo to mid + 1. If it's larger, set hi to mid. In both cases the middle index is no longer a candidate. If the values match, return the index; if the range becomes empty, there is no match.

Each comparison that doesn't find the target leaves at most half as many candidates. That gives O(log n) worst-case time when indexing and comparing take constant time. The iterative version needs only O(1) extra space because it keeps a few indices, not copies of the remaining items.

Example in Python

def binary_search(items, target):
    lo, hi = 0, len(items)
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if items[mid] == target:
            return mid
        if items[mid] < target:
            lo = mid + 1
        else:
            hi = mid
    return -1

With [3, 8, 12, 17, 24] and target 10, the middle values checked are 12 and then 8. At that point both bounds are 2, so the range is empty and the function returns -1.

When to use it and what to watch for

Binary search is useful for repeated lookups in already-sorted arrays. If the input is unsorted and you only need one lookup, a linear scan is generally cheaper than sorting first. A sorted linked list also doesn't give you the usual logarithmic running time: reaching the middle requires walking through nodes.

Pick one boundary convention and stick with it. For a half-open range, setting lo = mid instead of mid + 1 can leave a one-item range unchanged and make the loop run forever. Also decide what a match means when duplicates exist: returning as soon as you see equality finds a match, not necessarily the first one.

How it comes up in interviews

A common follow-up is to find the lower bound: the first index whose value is at least the target, or the array length if no such index exists. With half-open bounds, set hi = mid when the middle value is at least the target and keep searching to the left. The same idea works on possible answers rather than stored items. For example, to find the smallest batch size that fits a deadline, you can search for the first size that passes the deadline check, provided every larger size passes too. Explain what your bounds represent, and test empty input, one item, duplicates, and a missing target.