Assumptions and an explicit algorithm
The sequence must be sorted using the same ordering used by the search. This model assumes constant-time access to A[m], as in an array. The algorithm returns some matching index or “not found.” With duplicate values, it need not return the first or last occurrence; those require different variants and boundary conditions.[1]
- Start with L = 0 and R = n − 1. An empty sequence has L > R and compares no elements.
- While L ≤ R, choose m = L + ⌊(R − L)/2⌋ and compare the target with A[m].
- Equality ends the search. If the target is smaller, set R = m − 1; if larger, set L = m + 1.
- When L > R, no candidate remains. The target is absent from the sequence.
Each comparison between the target and a middle element counts here as one search step with three outcomes: less, equal or greater. A program may implement that using multiple operators such as == and <. Loop tests and index arithmetic also cost work. The step count is therefore not the exact number of all machine operations.
Why the search is correct and logarithmic
At the start of each round, if the target exists, at least one matching occurrence remains between L and R. Sortedness excludes all values on the wrong side after the middle comparison. If unequal, the checked middle element must also be removed. This preserves the statement while strictly shrinking the interval, preventing repeated checks of the same midpoint forever.[2]
H counts the maximum number of middle-element comparisons for this algorithm. After an unequal comparison, the larger remaining half contains at most ⌊n/2⌋ elements. Rounding is part of the statement.
The formula applies to positive integer n and the stated comparison convention. For n = 0, use H(0) = 0 separately. Best and worst cases differ: a match at the first midpoint needs just one middle comparison.
For n = 2ᵏ − 1, the deepest search path has exactly k levels; one more element, n = 2ᵏ, can require k + 1 levels. The recurrence explains the rounding boundary. Big-O notation alone instead describes the eventual growth of an upper bound, not an exact step count or fixed runtime.
Trace successful and unsuccessful searches
Find 60 among 15 sorted values
- Given
- A = [3, 7, 11, 14, 18, 21, 25, 29, 33, 37, 41, 46, 50, 54, 60], indices 0 through 14. Target 60.
- Find
- Trace the midpoints and count comparisons.
L = 0, R = 14: m = 7 and A[7] = 29. Since 60 is larger, set L = 8.
L = 8, R = 14: m = 11 and A[11] = 46. Then set L = 12.
L = 12, R = 14: m = 13 and A[13] = 54. Then set L = 14.
L = R = 14: m = 14 and A[14] = 60. The search succeeds.
Result: Four middle comparisons find 60 at index 14.
Check: The bound gives ⌈log2(15 + 1)⌉ = 4. Candidate counts are 15 → 7 → 3 → 1.
Rule out the missing value 20
- Given
- The same sorted sequence of 15 elements. Target 20.
- Find
- Establish that the target is absent.
m = 7, A[7] = 29: 20 is smaller, so R = 6.
Now m = 3, A[3] = 14: 20 is larger, so L = 4.
Now m = 5, A[5] = 21: 20 is smaller, so R = 4.
Now m = 4, A[4] = 18: 20 is larger, so L = 5. Since L > R, the loop ends.
Result: After four middle comparisons, the interval is empty: 20 is absent.
Check: 18 and 21 are the neighboring values around 20. No possible matching index was skipped.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Five exercises with solutions
Calculate · Foundation
How many middle comparisons are required in the worst case for n = 1,000?
Show hint
Compare 1,001 with neighboring powers of two.
Learning goal: Evaluate the integer comparison bound.
Show solution and calculation
29 = 512 < 1,001 ≤ 1,024 = 210, so ⌈log2(1,001)⌉ = 10.
This is a worst-case search-step count, not a runtime measurement.
Rearrange · Application
What is the largest sequence for which this algorithm always needs at most six middle comparisons?
Show hint
Solve ⌈log2(n + 1)⌉ ≤ 6 for integer n.
Learning goal: Infer maximum input size from a comparison budget.
Show solution and calculation
n + 1 ≤ 26 = 64, so n ≤ 63.
With 64 elements a seventh middle comparison may be needed.
Interpret · Foundation
For 1,023 elements, the target matches the first midpoint. Does one comparison contradict the worst-case bound of ten?
Show hint
An upper bound need not be attained for every target.
Learning goal: Distinguish the best case from the worst case.
Show solution and calculation
No.
This best-case instance needs one middle comparison. ⌈log2(1,024)⌉ = 10 describes the maximum needed in the worst case.
Error analysis · Application
Claim: n = 1,024 always needs at most log2(1,024) = 10 comparisons. Check the rounding boundary for this algorithm.
Show hint
The formula rounds up the logarithm of n + 1.
Learning goal: Detect an off-by-one error at a power of two.
Show solution and calculation
⌈log2(1,025)⌉ = 11.
Ten steps cover every case only through n = 1,023.
The additional value can create another search-path level.
Transfer · Application
Apply the algorithm to the unsorted sequence [1, 9, 3, 5, 7] with target 9. Why can the answer be wrong?
Show hint
The first midpoint contains 3. Which region is then discarded?
Learning goal: Justify sortedness through a counterexample.
Show solution and calculation
The middle at index 2 contains 3, so the algorithm moves right and discards the existing 9 at index 1.
It then checks 5 and 7 and incorrectly reports “not found.” Without sortedness, discarding a half is unjustified.
Check your understanding with the formula’s reference test.
7 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
When binary search is useful
Halving is especially useful for repeated queries on already sorted data. For a single query on unsorted data, sorting first can cost more than linear search. Accessing a middle element of a linked list is not automatically constant time either. Compare the whole workflow, including preparation and data structure. To consolidate understanding, practise a successful search, an unsuccessful search and a power-of-two boundary case.[1]
Sources for this explanation
Quanta · Updated November 7, 2026
- University of Toronto, CSC110, 18.1 Sorted Lists and Binary Search
- Cornell University, CS 2112, Loop invariants and binary search
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Added a DE/EN article covering a closed search interval, explicit comparison counting, halving argument, two traced examples and five exercises.
Cite this reference page
Quanta. Binary search: comparison bound and running time. Updated November 7, 2026. https://quanta-study.de/en/formula-sheet/binaersuche-laufzeit
When citing a specific scientific statement, also cite the specialist source linked to that statement.