Open In App

Is ternary search faster than binary search?

Binary search is a widely used algorithm for searching a sorted array. It works by repeatedly dividing the search space in half until the target element is found. Ternary search is a variation of binary search that divides the search space into three parts instead of two. This article explores the performance comparison between ternary search and binary search.

Binary Search Algorithm:

Ternary Search Algorithm:

Time Complexity of Binary Search:

Time Complexity of Ternary Search:

Complexity Comparison between Ternary Search and Binary Search:

Ternary search requires 4 comparisons, while binary search only needs a maximum of 2 comparisons per iteration.



The recurrence terms for both algorithms are:

Note: In logarithms, the lower the base, the higher the value.



Ternary search might seem faster than Binary search because log2(n) is greater than or equal to log3(N), but in reality, it’s not faster. When we look at the time complexity of algorithms, we usually don’t consider the constants. However, in Ternary search, these constants are larger compared to Binary search, making Ternary search slower in practice.

Proof of why Binary search is faster than Ternary Search:

For Binary Search, …1

For Ternary Search, …2

Using the property in equation 1 and 2, We get

…3

…4

Cb = Number of Comparison in each iteration of Binary Search = 2

Ct = Number of Comparison in each iteration of Ternary Search = 4

Substituting Cb and Ct in equation 3 and 4, we get

On Comparing the above equations 5 and 6,

Therefore, we can say that Binary search is faster than Ternary search.

Conclusion

Binary search algorithm is preferred over the Ternary search because Ternary search needs more comparisons, even though it reduces the number of steps. Hence, Binary search is better than Ternary search.


Article Tags :