Ternary search

A ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function.

The function
Assume we are looking for a maximum of $$f(x)$$ and that we know the maximum lies somewhere between $$A$$ and $$B$$. For the algorithm to be applicable, there must be some value $$x$$ such that
 * for all $$a, b$$ with $$A \leq a < b \leq x$$, we have $$f(a) < f(b)$$, and
 * for all $$a, b$$ with $$x \leq a < b \leq B$$, we have $$f(a) > f(b)$$.

Algorithm
Let $$f(x)$$ be a unimodal function on some interval $$[l; r]$$. Take any two points $$m_1$$ and $$m_2$$ in this segment: $$l < m_1 < m_2 < r$$. Then there are three possibilities: choice points $$m_1$$ and $$m_2$$:
 * if $$f(m_1) < f(m_2)$$, then the required maximum can not be located on the left side – $$[l; m_1]$$. It means that the maximum further makes sense to look only in the interval $$[m_1; r]$$
 * if $$f(m_1) > f(m_2)$$, that the situation is similar to the previous, up to symmetry. Now, the required maximum can not be in the right side – $$[m_2; r]$$, so go to the segment $$[l; m_2]$$
 * if $$f(m_1) = f(m_2)$$, then the search should be conducted in $$[m_1; m_2]$$, but this case can be attributed to any of the previous two (in order to simplify the code). Sooner or later the length of the segment will be a little less than a predetermined constant, and the process can be stopped.
 * $$m_1 = l + (r - l) / 3$$
 * $$m_2 = r - (r - l) / 3$$


 * Run time order
 * $$T(n) = T(2n/3) + 1

= \Theta(\log n)$$