Binary Search Steps Calculator

Find the worst case number of comparisons to search a sorted list with binary search.

Technology and NetUpdated 2026-11-192 min read
Binary Search Steps Calculator
A whole number from 1 to 10 to the power of 15.
Maximum comparisons
...
Log base 2 of n...
Items used...
Items removed per halving...

What it does

Binary search finds a value in a sorted list by comparing it with the middle element and discarding half of the remaining range each time. Its speed comes from the fact that the search space shrinks exponentially rather than one item at a time.

This tool takes the number of items in the list and returns the maximum number of comparisons in the worst case, along with the base two logarithm used to get it.

The default list has one million items. The base two logarithm is about 19.93, so the count is floor of 19.93 plus one, which is 20 comparisons.

The reasoning is simple. After k comparisons the remaining list has at most n divided by two to the power k items. The process stops when that falls below one, which happens when k exceeds log base two of n.

The estimate assumes a sorted list and a comparison model where examining the middle costs one step. It says nothing about the cost of sorting the data in the first place.

How to use

  1. Enter the number of items in the sorted list.
  2. Read the maximum comparisons as the main result.
  3. Check the base two logarithm to see the underlying scale.
  4. Double the list size and observe that the count rises by about one.
  5. Try 16 to see the five comparison case.
  6. Try a very large list to see how slowly the count grows.

Formula

Maximum comparisons = floor(log base 2 of n) + 1
n
Number of items in the sorted list.
log base 2
The power of two needed to reach n.
comparisons
Worst case number of middle element comparisons.

Some texts report ceil of log base two of n plus one as the bound, which differs by one at exact powers of two. This tool uses floor of log base two of n plus one, the form given in the cited lecture notes. The asymptotic complexity is logarithmic regardless of the constant.

Worked example

Example: one million items

The page opens with n = 1,000,000.

  1. Take the base two logarithm of one million, about 19.9316.
  2. Take the floor to get 19.
  3. Add one to get 20 comparisons in the worst case.
  4. Check by halving: after 19 halvings the list is down to at most one item.
  5. The tool reports comparisons 20 and log2 19.9316.
  6. A list twice as large would need 21 comparisons.

Reference table

Worst case comparisons by list size
List size nLog base 2 of nMaximum comparisons
21.00002
164.00005
1,000,00019.931620

Limits

  • Assumes the list is already sorted.
  • Counts comparisons of the middle element, not other operations.
  • Accepts whole list sizes from 1 to 10 to the power of 15.
  • The bound is for the worst case, not the average case.

Common mistakes

  • Applying binary search to an unsorted list.
  • Using log base 10 instead of log base 2.
  • Forgetting to add one to the floored logarithm.
  • Assuming the count grows linearly with list size.
  • Including the cost of sorting in the search count.

Try it yourself

Quick checks. Answers sit tucked under each question.

Worst case comparisons for 16 items?

5, since log base 2 of 16 is 4 and adding one gives 5.

What happens when the list size doubles?

The comparison count rises by about one.

Why is binary search fast?

Because each comparison removes half of the remaining candidates.

Questions

What is the worst case count?

Floor of log base 2 of n plus one comparisons.

Does the list need to be sorted?

Yes. Binary search relies on order to discard half the range.

What is the time complexity?

Logarithmic time, written as O(log n).

Why add one?

The final comparison can happen when one item remains.

How many comparisons for a billion items?

About thirty, which shows how slowly the count grows.

Does this include insertion cost?

No. It measures searching only.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.