# Ternary search

A ternary search algorithm is a technique in computer science for finding the minimum or maximum of a unimodal function. A ternary search determines either that the minimum or maximum cannot be in the first third of the domain or that it cannot be in the last third of the domain, then repeats on the remaining two thirds. A ternary search is an example of a divide and conquer algorithm (see search algorithm).

## The function

Assume we are looking for a maximum of ${\displaystyle f(x)}$ and that we know the maximum lies somewhere between ${\displaystyle A}$ and ${\displaystyle B}$. For the algorithm to be applicable, there must be some value ${\displaystyle x}$ such that

• for all ${\displaystyle a,b}$ with ${\displaystyle A\leq a, we have ${\displaystyle f(a), and
• for all ${\displaystyle a,b}$ with ${\displaystyle x\leq a, we have ${\displaystyle f(a)>f(b)}$.

## Algorithm

Let ${\displaystyle f(x)}$ be a unimodal function on some interval ${\displaystyle [l;r]}$. Take any two points ${\displaystyle m_{1}}$ and ${\displaystyle m_{2}}$ in this segment: ${\displaystyle l. Then there are three possibilities:

• if ${\displaystyle f(m_{1}), then the required maximum can not be located on the left side – ${\displaystyle [l;m_{1}]}$. It means that the maximum further makes sense to look only in the interval ${\displaystyle [m_{1};r]}$
• if ${\displaystyle 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 – ${\displaystyle [m_{2};r]}$, so go to the segment ${\displaystyle [l;m_{2}]}$
• if ${\displaystyle f(m_{1})=f(m_{2})}$, then the search should be conducted in ${\displaystyle [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.

choice points ${\displaystyle m_{1}}$ and ${\displaystyle m_{2}}$:

• ${\displaystyle m_{1}=l+(r-l)/3}$
• ${\displaystyle m_{2}=r-(r-l)/3}$
Run time order
${\displaystyle T(n)=T(2n/3)+1=\Theta (\log n)}$

### Recursive algorithm

def ternary_search(f, left, right, absolute_precision) -> float:
"""Left and right are the current bounds;
the maximum is between them.
"""
if abs(right - left) < absolute_precision:
return (left + right) / 2

left_third = (2*left + right) / 3
right_third = (left + 2*right) / 3

if f(left_third) < f(right_third):
return ternary_search(f, left_third, right, absolute_precision)
else:
return ternary_search(f, left, right_third, absolute_precision)


### Iterative algorithm

def ternary_search(f, left, right, absolute_precision) -> float:
"""Find maximum of unimodal function f() within [left, right].
To find the minimum, reverse the if/else statement or reverse the comparison.
"""
while abs(right - left) >= absolute_precision:
left_third = left + (right - left) / 3
right_third = right - (right - left) / 3

if f(left_third) < f(right_third):
left = left_third
else:
right = right_third

# Left and right are the current bounds; the maximum is between them
return (left + right) / 2