Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one question about binary search

Tags:

algorithm

Why people usually do binary search instead of triple search (divide the array into three parts each time) or even divide into ten parts each time?

like image 340
skydoor Avatar asked Feb 26 '10 00:02

skydoor


1 Answers

Because binary search results in the smallest amount of comparisons and lookups. For a simple intuition consider dividing into 4 parts each time.

[         |         |    .    |         ]
          v1        v2        v3

You now have done 3 lookups and have to compare the value you are searching for at worst against all three values. Compare this with two iterations of binary search:

[                   |    .              ]
                    v1
[                   |    .    |         ]
                    v1        v2

You have now narrowed the search range by the same amount, but have done only 2 lookups and 2 comparisons.

like image 169
Ants Aasma Avatar answered Jan 15 '23 17:01

Ants Aasma