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?
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- How to get a fixed number of evenly spaced points
- Space complexity of validation of a binary search
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to measure complexity of a string?
- Select unique/deduplication in SSE/AVX
- How to smooth the blocks of a 3D voxel world?
The reasoning is because you don't actually gain anything from it: the search is still
O(log n)
, just with a different base.It considerably simplifies the logic:
Versus a switch statement.
Because binary search results in the smallest amount of comparisons and lookups. For a simple intuition consider dividing into 4 parts each time.
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:
You have now narrowed the search range by the same amount, but have done only 2 lookups and 2 comparisons.
Binary allows for an easy comparison <= or >= or < or > (cannot remember what is typically used). It cleanly partitions the set and it is easy to come up with divisions. For arbitrary data sets, how would you divide into the different parts? How would you decide which part to put something in? Binary search takes O(log n) lookups to find. Adding more components would change that to something closer to O(m * log n) where m is the number of parts you are dividing into.
Jacob
No one's really mentioned that the comparison-operators implemented in all computers only compare two things at a time - if the computer could compare three objects at once, this would certainly make sense.
As it stands, to compare three values takes (at least) two operations.