Mathwords logoMathwords

Binary Search — Definition, Formula & Example

Binary search is a method for finding a value in a sorted list. You check the middle entry and then throw away the half that cannot contain your target.

Let a1≤a2≤⋯≤ana_1 \le a_2 \le \cdots \le a_n be a sorted array and tt a target. Set ℓ=1\ell = 1 and h=nh = n. While ℓ≤h\ell \le h, compute m=⌊(ℓ+h)/2⌋m = \lfloor (\ell + h)/2 \rfloor. If am=ta_m = t, return mm. If am<ta_m < t, set ℓ=m+1\ell = m + 1. Otherwise set h=m−1h = m - 1. If ℓ>h\ell > h, then tt is not in the array. Each pass removes ama_m and leaves at most half of the current range. So the loop runs at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 times, and the running time is O(log⁡n)O(\log n).

Key Formula

Cmax⁡(n)=⌊log⁡2n⌋+1C_{\max}(n) = \lfloor \log_2 n \rfloor + 1
Where:
  • Cmax⁡(n)C_{\max}(n) = Largest possible number of comparisons with a middle entry for a sorted list of length n
  • nn = Number of entries in the sorted list (a positive integer)
  • ⌊⋅⌋\lfloor \cdot \rfloor = Floor function: round down to the nearest integer

Worked Example

Problem: The sorted list 3, 8, 12, 17, 21, 26, 30, 34, 39, 45, 51, 58, 63, 70, 77 has 15 entries in positions 1 through 15. Use binary search to find the position of 63. Count the comparisons.
Step 1: Start with the whole list, so ℓ=1\ell = 1 and h=15h = 15. The middle position is 8, and a8=34a_8 = 34. Because 63>3463 > 34, the target must lie to the right, so set ℓ=9\ell = 9.
m=⌊1+152⌋=8m = \left\lfloor \frac{1 + 15}{2} \right\rfloor = 8
Step 2: Now the range is positions 9 to 15. The middle is position 12, and a12=58a_{12} = 58. Because 63>5863 > 58, set ℓ=13\ell = 13. The new middle is position 14, and a14=70a_{14} = 70. Because 63<7063 < 70, set h=13h = 13.
m=⌊9+152⌋=12,m=⌊13+152⌋=14m = \left\lfloor \frac{9 + 15}{2} \right\rfloor = 12, \qquad m = \left\lfloor \frac{13 + 15}{2} \right\rfloor = 14
Step 3: Only position 13 remains, and a13=63a_{13} = 63. That is a match. The search used 4 comparisons, which is exactly the worst-case bound for 15 entries.
m=⌊13+132⌋=13,⌊log⁡215⌋+1=3+1=4m = \left\lfloor \frac{13 + 13}{2} \right\rfloor = 13, \qquad \lfloor \log_2 15 \rfloor + 1 = 3 + 1 = 4
Answer: The value 63 is at position 13. Binary search found it in 4 comparisons. A left-to-right scan would have needed 13.

Why It Matters

In a discrete math or data structures course, binary search is the standard first example of the recurrence T(n)=T(n/2)+1T(n) = T(n/2) + 1, which solves to O(log⁡n)O(\log n). The gap in speed is large. A linear search through 1,000,000 sorted records may need 1,000,000 checks, but binary search needs at most 20. The same halving idea drives database indexes, the bisection method for finding roots of equations, and tools like git bisect that locate the commit where a bug first appeared.

Common Mistakes

Mistake: Running binary search on a list that is not sorted.
Correction: The method only works because sorting guarantees that everything left of the middle is smaller than everything right of it. Try searching the unsorted list 9, 2, 7, 4, 5 for 4. The middle entry is position 3, which holds 7. Since 4 < 7, the search keeps the left part {9, 2} and discards 7 and everything to its right, including the 4 at position 4. It then reports that 4 is not in the list, which is false. Sort the list first, or use a linear search.

Related Terms

  • Depth-First Search — Another search method, for graphs instead of lists
  • Algorithm — Defines the step-by-step procedures that binary search is an example of
  • Discrete Mathematics — Course where binary search's logarithmic running time is analyzed
  • Euclidean Algorithm — Another algorithm whose step count grows only logarithmically
  • Greedy Algorithm — A contrasting design strategy: local choices instead of halving
  • Bisection — Same halving idea applied to finding roots
  • Floor Function — Rounds down to find the middle index