%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

Binary Search & Comparison Count Calculator

Results update as you type.

Your numbers

Understanding the binary search and comparison count calculator — concept and principle

How many comparisons does it take to find an item or to sort a list? This calculator answers that for n items: the worst case of binary search on sorted data, the average of a plain linear scan, the theoretical minimum for any comparison-based sort, and the exact worst case of merge sort. It shows why sorting once and searching many times pays off.

Binary search halving a million sorted itemsOne million items are halved at every comparison, so at most 20 comparisons are needed, against 500,000.5 on average for a linear scan.Each comparison throws away half1,000,0001500,0002250,0003125,000462,5005⋮201 item leftBinary search20 comparisonsLinear search500,000.5 avg⌊log₂ 1,000,000⌋ + 1 = ⌊19.93⌋ + 1 = 20
Halving a million sorted items reaches one item in at most 20 comparisons.

How it is calculated

The principle

Each comparison in binary search halves the remaining range, so after j comparisons at most n ÷ 2ʲ candidates are left; the search ends once that drops below one, after about log₂ n steps. The sorting bound is an information argument: a sort must distinguish all n! possible orders, and each yes/no comparison can at best halve the possibilities, so at least log₂(n!) comparisons are needed in the worst case. By Stirling's formula log₂(n!) ≈ n log₂ n − 1.443n, which is why no comparison sort can beat Θ(n log n). Merge sort comes within a few percent of this bound.

Worked example

  1. Input: n = 1,000,000.
  2. Binary search: log₂ 10⁶ ≈ 19.93, ⌊19.93⌋ + 1 = 20 comparisons.
  3. Linear search: (1,000,000 + 1) ÷ 2 = 500,000.5 comparisons on average.
  4. Lower bound: log₂(1,000,000!) ≈ 18,488,884.82.
  5. Merge sort: ⌈log₂ 10⁶⌉ = 20, so 10⁶ × 20 − 2²⁰ + 1 = 20,000,000 − 1,048,576 + 1 = 18,951,425.
How Worst-case comparisons (binary search) changes with Number of itemsHow Worst-case comparisons changes with…18192021250,000500,000750,0001e+061.25e+061.5e+062e+06Example: 20Number of items
Each point is the calculator re-run with only “Number of items” changed; the gold point is the default example. Change the inputs above and this chart follows your numbers.
Number of itemsWorst-case comparisons (binary search)
250,00018
500,00019
750,00020
1e+0620
1.25e+0621
1.5e+0621
2e+0621

How to read the result

Binary search is about 25,000 times cheaper than a linear scan here, but it needs sorted data. Sorting costs roughly 19 million comparisons, so if you will search the same data fewer than a few dozen times, a linear scan may be cheaper overall. Merge sort's worst case is only about 2.5% above the lower bound, so there is little room left to improve comparison counts.

Common mistakes

Keep in mind: These are comparison counts, not run times. Memory access, branch prediction and data movement often matter more.

Frequently asked questions

Why is it ⌊log₂ n⌋ + 1 and not log₂ n?

The count must be a whole number, and the last comparison is needed even when one candidate remains. For n = 1,000,000 that gives 20.

How many steps for a billion items?

Only 30, because 2³⁰ is just over a billion.

Is a hash table faster than binary search?

On average, yes: a lookup takes a constant number of probes, but a hash table cannot answer range or nearest-value queries.

Sources: D. E. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, Addison-Wesley (binary search; minimum-comparison sorting) · T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, MIT Press (lower bounds for sorting)

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →