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/C++ Code // 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
11 min read
Searching in Binary Search Tree (BST)
Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
7 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. FeatureBinary Search Tree (BST)Ternary Search Tree (T
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
Iterative searching in Binary Search Tree
Given a Binary Search Tree and a key, the task is to find if the node with a value key is present in the BST or not. Example: Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInput: Root of the below BST Output: FalseExplanation: 14 is not present i
6 min read
Linear Search vs Binary Search
Prerequisite: Linear SearchBinary SearchLINEAR SEARCH Assume that item is in an array in random order and we have to find an item. Then the only way to search for a target item is, to begin with, the first position and compare it to the target. If the item is at the same, we will return the position
11 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
What is the difference between Binary Search and Jump Search?
Binary Search and Jump Search are two popular algorithms used for searching elements in a sorted array. Although they both try to identify a target value as quickly as possible, they use distinct approaches to get there. In this article, we will discuss the difference between binary search and jump
2 min read
In which scenario is ternary search useful?
Ternary search is a search algorithm that divides the search space into three parts instead of two, like binary search. This makes it more efficient for certain types of problems. When to Use Ternary Search:Ternary search is particularly useful when the search space is large and the function being s
2 min read