Open In App

Is ternary search faster than binary search?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • 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.

Ternary Search Algorithm:

  • 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.

Time Complexity of Binary Search:

  • Each iteration divides the search space in half.
  • Therefore, the number of iterations required is log2(n).

Time Complexity of Ternary Search:

  • 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, [Tex]T_{b}(N) = C_{b} \cdot \log_{2}(N)[/Tex] …1

For Ternary Search, [Tex] T_{t}(N) = C_{t} \cdot \log_{3}(N) [/Tex]…2

Using the property [Tex] \log_{b}(N) = \frac{\log_{e}(N)}{\log_{e}(b)} [/Tex]in equation 1 and 2, We get

[Tex]T_{b}(N) = C_{b} * \frac{\ln(N)}{\ln(2)} = \frac{C_{b}}{\ln(2)}*\ln(N) = 1.4426 * C_{b} * \ln(N) [/Tex]…3

[Tex]T_{t}(N) = C_{t} * \frac{\ln(N)}{\ln(3)} = \frac{C_{t}}{\ln(3)}*\ln(N) = 0.9102 * C_{t} * \ln(N) [/Tex]…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

[Tex]T_{b}(N) = 1.4426*2* \ln(N) = 2.885*\ln(N) [/Tex]

[Tex]T_{t}(N) = 0.9102*4* \ln(N) = 3.6408*\ln(N) [/Tex]

On Comparing the above equations 5 and 6,

[Tex]T_{b}(N) < T_{t}(N)[/Tex]

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads