Binary search and Ternary search algorithms are used to search an element in a sorted array. Binary search reduces the array by 1/3 on each iteration whereas Ternary search reduced array size by 1/3 on each iteration.
The Time complexity of Binary Search is log2(N).
The Time complexity of Ternary Search is log3(N).
Ternary search should be faster than the Binary search since log2(N) >= log3(N) but this is not the case. When we calculate the Time complexity of any algorithm, we generally ignore the constants. But the constants in Ternary search are relatively larger than Binary search. Due to this, the Ternary search is slower.
Cb = Number of Comparision in each iteration of Binary Search = 2
Ct = Number of Comparision in each iteration of Ternary Search = 4
Substituting Cb and Ct in equation 3 and 4, we get
On Comparing equation 5 and 6,
Thus, we can say that Binary search is faster than Ternary search.
This happens because of the increase in the number of comparisons in Ternary search. In simple words, the reduction in the number of iterations in Ternary search is not able to compensate for the increase in comparisons.
Hence, Binary search algorithm is preferred over the Ternary search.