Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use binary search if there's ternary search?

I recently heard about ternary search in which we divide an array into 3 parts and compare. Here there will be two comparisons but it reduces the array to n/3. Why don't people use this much?

like image 611
mousey Avatar asked Aug 17 '10 00:08

mousey


People also ask

Why is binary search better than ternary?

From the first look, it seems the ternary search does less number of comparisons as it makes Log3n recursive calls, but binary search makes Log2n recursive calls.

Which is better binary or ternary search?

Yes, in terms of complexity, binary search is better than ternary search.

Why would you prefer to use binary search instead of using sequential search?

Case 2: When the data is sorted, a Binary Search will be more time efficient as it will only take O(logn) time, while the Sequential Search will still take O(n) time. Show activity on this post. Binary search is always better. But binary search always requires to array should be sorted.

Why is binary search preferred?

The main advantage of using binary search is that it does not scan each element in the list. Instead of scanning each element, it performs the searching to the half of the list. So, the binary search takes less time to search an element as compared to a linear search.


2 Answers

Actually, people do use k-ary trees for arbitrary k.

This is, however, a tradeoff.

To find an element in a k-ary tree, you need around k*ln(N)/ln(k) operations (remember the change-of-base formula). The larger your k is, the more overall operations you need.

The logical extension of what you are saying is "why don't people use an N-ary tree for N data elements?". Which, of course, would be an array.

like image 59
Borealid Avatar answered Oct 08 '22 08:10

Borealid


A ternary search will still give you the same asymptotic complexity O(log N) search time, and adds complexity to the implementation.

The same argument can be said for why you would not want a quad search or any other higher order.

like image 34
Akusete Avatar answered Oct 08 '22 08:10

Akusete