Is ternary search faster than binary search?
Last Updated :
15 Feb, 2024
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.
- Divide the search space into two equal halves.
- Compare the target element with the middle element.
- If the target element is equal to the middle element, return its index.
- If the target element is less than the middle element, search in the left half.
- If the target element is greater than the middle element, search in the right half.
- Divide the search space into three equal parts.
- Compare the target element with the two middle elements.
- If the target element is equal to one of the middle elements, return its index.
- If the target element is less than the left middle element, search in the left third.
- If the target element is between the left and right middle elements, search in the middle third.
- If the target element is greater than the right middle element, search in the right third.
- Each iteration divides the search space in half.
- Therefore, the number of iterations required is log2(n).
- Each iteration divides the search space into three parts.
- Therefore, the number of iterations required is log3(n).
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:
- For Binary Search: T1(n) = 2clog2(n) + O(1) (where c is a constant)
- For Ternary Search: T2(n) = 4clog3(n) + O(1) (where c is a constant)
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, T_{b}(N) = C_{b} \cdot \log_{2}(N) ...1
For Ternary Search,
T_{t}(N) = C_{t} \cdot \log_{3}(N)
...2
Using the property
\log_{b}(N) = \frac{\log_{e}(N)}{\log_{e}(b)}
in equation 1 and 2, We get
T_{b}(N) = C_{b} * \frac{\ln(N)}{\ln(2)} = \frac{C_{b}}{\ln(2)}*\ln(N) = 1.4426 * C_{b} * \ln(N)
...3
T_{t}(N) = C_{t} * \frac{\ln(N)}{\ln(3)} = \frac{C_{t}}{\ln(3)}*\ln(N) = 0.9102 * C_{t} * \ln(N)
...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
T_{b}(N) = 1.4426*2* \ln(N) = 2.885*\ln(N)
T_{t}(N) = 0.9102*4* \ln(N) = 3.6408*\ln(N)
On Comparing the above equations 5 and 6,
T_{b}(N) < T_{t}(N)
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.
Similar Reads
Is there any search faster than Binary Search? No, there is no search faster than Binary Search. Binary Search is the fastest searching algorithm for sorted data. It takes O(log2N) time to search any element in the sorted search space. In this article, we will discuss about how Binary Search works, it time complexity, comparison with other searc
3 min read
Why is Binary Search preferred over Ternary Search? The following is a simple recursive Binary Search function in C++ taken from here. C++ // CPP program for the above approach #include <bits/stdc++.h> using namespace std; // A recursive binary search function. It returns location of x in // given array arr[l..r] is present, otherwise -1 int b
11 min read
Binary Search Tree vs Ternary Search Tree For effective searching, insertion, and deletion of items, two types of search trees are used: the binary search tree (BST) and the ternary search tree (TST). Although the two trees share a similar structure, they differ in some significant ways. Binary Search Tree Vs Ternary Search Tree FeatureBina
3 min read
Which is faster between binary search and linear search? In computer science, search algorithms are used to locate a specific element within a data structure. Two commonly used search algorithms are binary search and linear search. Understanding their relative speeds is crucial for optimizing search operations. Let's compare the speed of Binary Search and
2 min read
Linear Search vs Binary Search Prerequisites:Linear SearchBinary Search Important Differences Linear Search Binary SearchIn linear search input data need not to be in sorted.In binary search input data need to be in sorted order.It is also called sequential search.It is also called half-interval search.The time complexity of line
7 min read
Interpolation search vs Binary search Interpolation search works better than Binary Search for a Sorted and Uniformly Distributed array. Binary Search goes to the middle element to check irrespective of search-key. On the other hand, Interpolation Search may go to different locations according to search-key. If the value of the search-k
7 min read