Ternary Search
I haven’t yet encountered a problem where this is needed. It is very similar to Binary Search, except that it reduces the time complexity even more, (log base 3).
- Today, first problem https://codeforces.com/problemset/problem/1999/G2 that requires this
Resources
- https://cp-algorithms.com/num_methods/ternary_search.html
- https://www.geeksforgeeks.org/ternary-search/
Version 1
This implements ternary search given an array ar
.
Version 2
Used to solve for Unimodal Function.
We are given a function which is Unimodal Function on an interval . The task is to find the maximum of function on the interval .
- Taken from https://cp-algorithms.com/num_methods/ternary_search.html
- Is this correct? Seems to only removes 1/3 of option? (i.e. 2/3 time)
- It gets updated either to or , which are both 2/3 of option
- In this case, the nature of the problem forces us to end up doing a binary decision. We can’t be sure that it’s in the smaller interval (just come up with a counterexample)
So that seems slower than binary search??
- Note that the example above is a Continuous Function. If it was discrete, we can do binary search using the tricks that erichto taught you for Binary Search