Chapter 16: Searching Algorithms
16.06Beginner / Intermediate5 topics24 min5 interactive animations

Other searches

Binary search is not the only way to search sorted data. These alternatives trade probes for different strengths: cheap backward steps, unknown sizes, uniform data, addition-only arithmetic, or finding the top of a hill.

Learning goals

  • Trace jump, exponential, interpolation and Fibonacci search
  • Compare their probe counts and the data each one suits
  • Use ternary search to find the maximum of a unimodal sequence

Prerequisites

Binary searchSquare roots and logarithms
16.06.5

Ternary search for unimodal functions

A unimodal sequence strictly rises and then strictly falls. Compare two interior points m1 < m2. If f(m1) < f(m2), the maximum cannot lie in [lo, m1]. Otherwise it cannot lie in [m2, hi]. Each step removes about a third of the window.

Ternary search is mainly used on functions (including real-valued ones) to find a maximum or minimum. For searching a sorted array, binary search makes fewer comparisons.

Java
1// real-valued version: maximise a unimodal f on [lo, hi]
2for (int iter = 0; iter < 100; iter++) {
3 double m1 = lo + (hi - lo) / 3, m2 = hi - (hi - lo) / 3;
4 if (f(m1) < f(m2)) lo = m1;
5 else hi = m2;
6}
7return (lo + hi) / 2;

Worst

O(log n)

Space

O(1)

Ternary search (maximum of a unimodal array)

Interactive · edit the input

Your input

Try

int[] arr · length 9

1
4
9
15
22
18
12
7
3
012345678
Being comparedNot a matchDiscardedMatch / answer
Setup

Comparing two inner points tells us which third cannot contain the maximum.

Step 1 / 14
Java
1public static int ternaryMax(int[] arr) {
2 int lo = 0, hi = arr.length - 1;
3 while (hi - lo > 2) {
4 int m1 = lo + (hi - lo) / 3;
5 int m2 = hi - (hi - lo) / 3;
6 if (arr[m1] < arr[m2]) {
7 lo = m1 + 1; // max is not in [lo, m1]
8 } else {
9 hi = m2 - 1; // max is not in [m2, hi]
10 }
11 }
12 int best = lo;
13 for (int i = lo + 1; i <= hi; i++) {
14 if (arr[i] > arr[best]) best = i;
15 }
16 return best;
17}

Live state

0 comparisons

lo

0

hi

8

No condition evaluated on this step
Execution trace (dry run table)Show 14 steps
StepLineStateWhat happens
1L2lo=0 hi=8Comparing two inner points tells us which third cannot contain the maximum.
2L3lo=0 hi=8Window still has more than three cells.
3L6lo=0 hi=8 m1=2 m2=6 arr[m1]=9 arr[m2]=12m1 is lower, so m1 is on the rising slope. Drop [lo, m1].
4L7lo=3 hi=8lo = 3.
5L3lo=3 hi=8Window still has more than three cells.
6L6lo=3 hi=8 m1=4 m2=7 arr[m1]=22 arr[m2]=7m2 is not higher, so m2 is on the falling slope. Drop [m2, hi].
7L9lo=3 hi=6hi = 6.
8L3lo=3 hi=6Window still has more than three cells.
9L6lo=3 hi=6 m1=4 m2=5 arr[m1]=22 arr[m2]=18m2 is not higher, so m2 is on the falling slope. Drop [m2, hi].
10L9lo=3 hi=4hi = 4.
11L3lo=3 hi=4Three or fewer cells left. Finish with a short scan.
12L12lo=3 hi=4 best=3Start with best = 3.
13L14lo=3 hi=4 best=3 i=4New best = 4.
14L16return=4return 4: the maximum is 22, found with 4 comparisons.

Practice ladder

Now try it yourself

Easy

  • Implement jump search and count comparisons for n = 100
  • Find an element in a sorted array of unknown size

Medium

  • Peak of a bitonic array with ternary search
  • Compare probe counts of binary vs interpolation search on random uniform data

Advanced

  • Minimise a convex cost function of a real parameter
  • Fibonacci search on a disk-backed array and measure seeks

Key takeaways

  • Jump search is O(√n); exponential search is O(log i); interpolation search is O(log log n) on uniform data.
  • Fibonacci search avoids division; ternary search finds the extremum of a unimodal function.
  • For general sorted arrays in memory, plain binary search is still the default.