Why is Binary Search preferred over Ternary Search?

The following is a simple recursive Binary Search function in C++ taken

[pastacode lang=”cpp” manual=”%2F%2F%20A%20recursive%20binary%20search%20function.%20It%20returns%20location%20of%20x%20in%0A%2F%2F%20given%20array%20arr%5Bl..r%5D%20is%20present%2C%20otherwise%20-1%0Aint%20binarySearch(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r%2C%20int%20x)%0A%7B%0A%20%20%20if%20(r%20%3E%3D%20l)%0A%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20the%20element%20is%20present%20at%20the%20middle%20itself%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3D%3D%20x)%20%20return%20mid%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20element%20is%20smaller%20than%20mid%2C%20then%20it%20can%20only%20be%20present%0A%20%20%20%20%20%20%20%20%2F%2F%20in%20left%20subarray%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3E%20x)%20return%20binarySearch(arr%2C%20l%2C%20mid-1%2C%20x)%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Else%20the%20element%20can%20only%20be%20present%20in%20right%20subarray%0A%20%20%20%20%20%20%20%20return%20binarySearch(arr%2C%20mid%2B1%2C%20r%2C%20x)%3B%0A%20%20%20%7D%0A%20%20%0A%20%20%20%2F%2F%20We%20reach%20here%20when%20element%20is%20not%20present%20in%20array%0A%20%20%20return%20-1%3B%0A%7D%20″ message=”C++” highlight=”” provider=”manual”/]

 

The following is a simple recursive Ternary Search function in C++.

[pastacode lang=”cpp” manual=”%2F%2F%20A%20recursive%20ternary%20search%20function.%20It%20returns%20location%20of%20x%20in%0A%2F%2F%20given%20array%20arr%5Bl..r%5D%20is%20present%2C%20otherwise%20-1%0Aint%20ternarySearch(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r%2C%20int%20x)%0A%7B%0A%20%20%20if%20(r%20%3E%3D%20l)%0A%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20mid1%20%3D%20l%20%2B%20(r%20-%20l)%2F3%3B%0A%20%20%20%20%20%20%20%20int%20mid2%20%3D%20mid1%20%2B%20(r%20-%20l)%2F3%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20present%20at%20the%20mid1%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid1%5D%20%3D%3D%20x)%20%20return%20mid1%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20present%20at%20the%20mid2%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid2%5D%20%3D%3D%20x)%20%20return%20mid2%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20present%20in%20left%20one-third%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid1%5D%20%3E%20x)%20return%20ternarySearch(arr%2C%20l%2C%20mid1-1%2C%20x)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20present%20in%20right%20one-third%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid2%5D%20%3C%20x)%20return%20ternarySearch(arr%2C%20mid2%2B1%2C%20r%2C%20x)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20present%20in%20middle%20one-third%0A%20%20%20%20%20%20%20%20return%20ternarySearch(arr%2C%20mid1%2B1%2C%20mid2-1%2C%20x)%3B%0A%20%20%20%7D%0A%20%20%20%2F%2F%20We%20reach%20here%20when%20element%20is%20not%20present%20in%20array%0A%20%20%20return%20-1%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/] [ad type=”banner”]

Which of the above two does less comparisons in worst case?

From the first look, it seems the ternary search does less number of comparisons as it makes Log3n recursive calls, but binary search makes Log2n recursive calls. Let us take a closer look.
The following is recursive formula for counting comparisons in worst case of Binary Search.

T(n) = T(n/2) + 2, T(1) = 1

The following is recursive formula for counting comparisons in worst case of Ternary Search.

T(n) = T(n/3) + 4, T(1) = 1

In binary search, there are 2Log2n + 1 comparisons in worst case. In ternary search, there are 4Log3n + 1 comparisons in worst case.

Time Complexity for Binary search = 2clog2n + O(1)

Time Complexity for Ternary search = 4clog3n + O(1)

Therefore, the comparison of Ternary and Binary Searches boils down the comparison of expressions 2Log3n and Log2n . The value of 2Log3n can be written as (2 / Log23) * Log2n . Since the value of (2 / Log23) is more than one, Ternary Search does more comparisons than Binary Search in worst case.

[ad type=”banner”]