What is binary search?
Last updated: 2026-11-26
Binary search finds a target value in a sorted list by checking the middle element and discarding the half that cannot hold the target, so each miss rules out about half of what is left. The list has to be sorted first, because the discard step has no other way to know which half to throw away. Below, the steps are counted on a list of 16 numbers.
Start learning No account needed
1. One search, step by step
The list is 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, which is 16 numbers in order. Searching for 15 starts with the interval of indexes 0 to 15. Step 1: middle index 7, value 15. The target equals the middle value, so the search ends after one comparison.
The binary search entry in the NIST Dictionary of Algorithms and Data Structures gives this method: start with an interval that covers the whole sorted array, look at the middle item, move to the lower half if the target is smaller and to the upper half if it is larger, and repeat until the value turns up or nothing is left. Wikipedia's article agrees, and adds that an empty remaining half means the value is absent.
2. The counts, including one miss
The same search runs for four targets in scripts/what-is-binary-search-worked-example.py in the Professor Turing repository, and a linear scan runs beside it for comparison. A binary step here is one look at a middle value. The linear scan checks values from the front until it finds the target or runs out, and it makes no use of the order.
| Target | Binary search steps | Linear scan steps |
|---|---|---|
| 1, the first value | 4 | 1 |
| 15, the middle value | 1 | 8 |
| 31, the last value | 5 | 16 |
| 8, not in the list | 4, ending on an empty interval | 16 |
Two rows repay a look. Target 1 is the one row where the linear scan takes fewer steps: binary steps 4, linear steps 1, because the value sits at the front. Target 31 needs binary steps 5, linear steps 16, and its 5 comparisons match the bound floor(log2 16) + 1 = 5, the most this list can ask of binary search. Its low, high and middle indexes go 0, 15, 7, then 8, 15, 11, then 12, 15, 13, then 14, 15, 14, and last 15, 15, 15, with middle values 15, 23, 27, 29 and 31. Target 8 is absent, and the search reports that after 4 comparisons by ending with an empty interval.
Sorting the numbers first is what buys that bound. The Wikipedia article states the requirement plainly: "the array must be sorted first to be able to apply binary search."
3. A common misconception: the idea is easy, so the code is easy
The idea is short, which makes a first version easy to trust. Wikipedia's section on implementation issues reports that when Jon Bentley assigned the problem to professional programmers taking a course, "ninety percent failed to provide a correct solution", even with hours to work on it. It adds that Bentley's own published version held an overflow error for over twenty years, and that the Java library had the same bug for more than nine years. The sum low + high can exceed the largest value an integer type holds, so (low + high) / 2 can fail on very large arrays. Computing low + (high - low) / 2 avoids that when both indexes are non-negative. The article also says most of the programmers who got it wrong made a mistake in the exit conditions, and that a badly defined exit can loop forever.
So write down low, high and the middle at each step, as section 2 does for the target 31. Tracing them by hand is a check you can run on your own code.
Test yourself
Work each answer out before you open it.
Question 1. On the 16-number list, what is the most comparisons binary search can need, and which target in the table reaches that?
Answer to question 1
5 comparisons, the bound floor(log2 16) + 1 = 5. Target 31 reaches it: each comparison leaves the target in the upper half, until only the last index remains.
Question 2. The list is shuffled into random order by mistake and nobody re-sorts it. What does binary search do when you search for a value that is present?
Answer to question 2
It runs and may answer "absent". A discarded half can hold the target when the values are out of order, so the method misses silently. Sorting first is not a refinement of the method; it is what makes the discard step correct.
Question 3. A teammate's program computes the middle as (low + high) / 2 with fixed-size integers and fails on a very large array. Name the fix.
Answer to question 3
Compute low + (high - low) / 2. The sum low + high overflows when it passes the largest value the integer type holds; high - low stays in range when both indexes are non-negative. Wikipedia and the NIST entry both describe this problem.
What the sources found, and their limits
NIST Dictionary of Algorithms and Data Structures, "binary search". Paul E. Black, entry modified 21 April 2022. It gives the method described in section 1, the note "Run time is O(ln n)", and the overflow trap in the middle calculation with the low + (high - low) / 2 fix. Source: xlinux.nist.gov/dads/HTML/binarySearch.html, a NIST staff work, not subject to copyright in the United States, read on 2026-11-25. Limits: the entry is a dictionary definition, so it carries no worked counts; the step counts here are ours, computed by the script.
Wikipedia, "Binary search algorithm". It gives the definition, the worst-case bound of floor(log2(n) + 1) loop iterations, the sorted-array requirement, and the history in section 3, including Bentley's report and the overflow bug. Source: en.wikipedia.org/wiki/Binary_search_algorithm, CC BY-SA 4.0, read on 2026-11-25. Limits: the article analyzes arrays in general; the 16-item list, the four targets and the counts in section 2 are ours, computed by the script.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start. It's an AI tutor, free to try.
Bring the search you are stuck on, such as where your low and high bounds went wrong. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer.
It does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.
FAQ
Why does binary search need a sorted list? Each step discards half the interval after comparing the target with the middle value, and that comparison only tells you which half to discard when the values are in order. On an unsorted list the discarded half could hold the target, so the search would silently miss it.
How many comparisons does binary search need? At most floor(log2 n) + 1 loop iterations in the worst case, where n is the list length. For a list of 16 numbers that bound is floor(log2 16) + 1 = 5, and the worked example on this page shows a target that uses all 5.
Start with what you're working on
Tell the tutor which algorithm you are studying and let it ask you the questions.
Start learning No account needed
See also: how to prepare for a coding interview · how to study for a computer science exam · how to test yourself · array and linked list · all study guides