Binary search notes

Back to Binary search

Number of indicator tests for the binary search

Let

and

It can be shown that

for all . By induction,

Then

Therefore tests are sufficient to pinpoint a single element. Similarly,

Therefore tests are necessary to pinpoint a single element.

Number of indicator tests for the exponential binary search

Let

where , and is the searched element. The binary search performs

tests. The initial exponential search performs tests. Therefore the total number of tests is . We may then solve the inequalities as

Comparison for a known search interval

We may now compare the number of comparisons made in the exponential binary search, and the binary search, if an upper bound is known for the search interval . Let us compare the worst-case comparison counts approximately:

Therefore, the exponential search uses less comparisons if the searched element is at most . For example, if , then that is , which seems reasonable. However, if , such as in computing the base-2 logarithm for a 64-bit integer, then , which is not so reasonable; to compute the logarithm of requires more than twice the comparisons than the binary search. It would be nice if the number of comparisons in the exponential binary search could be brought down close to that of the binary search; then we could consistently use the exponential binary search everywhere.