ArticleslgStudy

computer science

Ternary search

Ternary search is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Ternary search rather than just read about it. In short: 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 ) {\displaystyle f(x)} and that we know the maximum lies somewhere between A {\displaystyle A} and B {\displaystyle B} .

Key takeaways

  • Ternary search belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Ternary search to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Ternary search from memory before moving on to harder problems.

Reference excerpt

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 ) {\displaystyle f(x)} and that we know the maximum lies somewhere between A {\displaystyle A} and B {\displaystyle B} . For the algorithm to be applicable, there must be some value x {\displaystyle x} such that

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

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

if f ( m 1 ) < f ( m 2 ) {\displaystyle f(m_{1})<f(m_{2})} , then the required maximum can not be located on the left side – [ l ; m 1 ] {\displaystyle [l;m_{1}]} . It means that the maximum further makes sense to look only in the interval [ m 1 ; r ] {\displaystyle [m_{1};r]}

if f ( m 1 ) > f ( m 2 ) {\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 – [ m 2 ; r ] {\displaystyle [m_{2};r]} , so go to the segment [ l ; m 2 ] {\displaystyle [l;m_{2}]}

if f ( m 1 ) = f ( m 2 ) {\displaystyle f(m_{1})=f(m_{2})} , then the search should be conducted in [ m 1 ; m 2 ] {\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 m 1 {\displaystyle m_{1}} and m 2 {\displaystyle m_{2}} :

m 1 = l + ( r − l ) / 3 {\displaystyle m_{1}=l+(r-l)/3}

m 2 = r − ( r − l ) / 3 {\displaystyle m_{2}=r-(r-l)/3}

Run time order

T ( n ) = T ( 2 n / 3 ) + O ( 1 ) = Θ ( log ⁡ n ) {\displaystyle T(n)=T(2n/3)+O(1)=\Theta (\log n)} (by the Master Theorem)

Recursive algorithm

Iterative algorithm

See also Newton's method in optimization (can be used to search for where the derivative is zero) Golden-section search (similar to ternary search, useful if evaluating f takes most of the time per iteration) Binary search algorithm (can be used to search for where the derivative changes in sign) Interpolation search Exponential search Linear search

References

Worked examples

Example 1 — a first encounter with Ternary search

Start with the simplest possible case. Write down what Ternary search claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Ternary search before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Ternary search ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Ternary search

In research
Ternary search appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Ternary search in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Ternary search is common in secondary-school and first-year university syllabi. It links to neighbouring topics Optimization algorithms and methods, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Ternary search outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Ternary search” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Ternary search in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Ternary search means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Ternary search out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Ternary search in simple terms?

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 ) {\displaystyle f(x)} and that we know the maximum lies somewhere between A {\displaystyle A} and B {\displaystyle B} .

Why does Ternary search matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Ternary search?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Ternary search.

Tags

  • Optimization algorithms and methods
  • Search algorithms

Keep exploring