3.6.10 · D4Sorting & Searching

Exercises — Linear time selection — median of medians algorithm

2,155 words10 min readBack to topic

Every symbol we use is already defined in the parent, but here is the one-line reminder so you never guess:

  • ::: the number of elements in the array right now.
  • ::: the rank we want; the answer is the element that would sit at sorted position (counting from 1).
  • ::: the pivot chosen by the median-of-medians rule.
  • ::: the total work (number of basic steps) the algorithm does on an array of size .

Level 1 — Recognition

L1.1

State whether each is TRUE or FALSE, and say why in one sentence. (a) Selection needs the array to be fully sorted first. (b) With the selection problem returns the minimum. (c) The MoM recurrence is .

Recall Solution

(a) FALSE. The whole point is to find one order statistic without paying the of sorting; we only partition, never fully sort. (b) TRUE. Sorted position 1 is the smallest element, i.e. the minimum. (c) TRUE. This is exactly the recurrence proven in the parent note — one recursive call on the medians, one on the surviving side of size , plus linear work .

L1.2

Fill the blanks. MoM splits elements into groups of ___; at least ___ elements are guaranteed ; the two recursive fractions are and ___, and they must sum to something ___ than 1 for linearity.

Recall Solution

Groups of 5; at least elements ; the second fraction is ; their sum must be less than 1. Here . ✓


Level 2 — Application

L2.1

Array (n=15): [25, 4, 13, 8, 40, 2, 17, 33, 6, 11, 29, 7, 1, 20, 9]. Find the MoM pivot by hand (groups of 5, in the order given).

Recall Solution

Groups of 5 (in order): [25,4,13,8,40] [2,17,33,6,11] [29,7,1,20,9] Sort each, take the middle (3rd of 5):

  • [4,8,13,25,40] → median 13
  • [2,6,11,17,33] → median 11
  • [1,7,9,20,29] → median 9 Median of medians of [13,11,9]: sorted [9,11,13] → middle is 11. So .

L2.2

Using from L2.1, partition the array into (less than ), (equal), (greater). If we wanted , which side do we recurse into and with what new ?

Recall Solution

Walk the array and bucket each element around :

  • (< 11): [4, 8, 2, 6, 7, 1, 9].
  • (= 11): [11].
  • (> 11): [25, 13, 40, 17, 33, 29, 20]. Positions: occupies ranks , occupies rank , occupies ranks . Since and , we have but , so lands inside → the answer is . No further recursion needed. (Check: 11 is indeed the 8th smallest — the median — of the 15 numbers.)

Level 3 — Analysis

L3.1

Prove the " guaranteed smaller" bound from scratch for . How many group-medians are ? How many total elements are guaranteed ? Draw the picture.

Figure — Linear time selection — median of medians algorithm
Recall Solution

With there are groups, so 5 group-medians. is the median of these 5, so at least 3 of them are (the median itself plus the two below it — that is ). Now look at each of those 3 groups whose median is (blue-shaded column in the figure). Inside such a group, the median plus the 2 elements smaller than it are all — that is 3 of the 5 elements per group. Guaranteed smaller: elements . Compare to the formula . Our exact count ✓ — the figure is a lower bound (it drops boundary/partial groups), and the true count is a bit better.

L3.2

Given the guarantee, what is the largest the surviving (recursed) side can be, for general ? Show the arithmetic.

Recall Solution

At least elements are and (by mirror symmetry) at least are . So the side we do not recurse into contains at least elements, meaning the side we do recurse into is at most That is exactly the second recursive size in the recurrence.


Level 4 — Synthesis

L4.1

Solve the recurrence by the substitution method. Find the smallest constant (in terms of ) for which holds, and conclude the running time.

Recall Solution

Guess for all and substitute: Combine the -terms: , so We want this . So require , i.e. , i.e. Smallest valid constant: . Then . Linear worst-case.

L4.2

Redo L4.1 for group size 7, whose recurrence is . Find and confirm linearity.

Recall Solution

Substitute : (Since .) Require : , so . Smallest constant , giving — still linear, with a smaller constant factor than the of group-5 (though group-5 keeps per-group median-finding cheaper, which is why 5 wins in practice).


Level 5 — Mastery

L5.1

Why does group size 3 fail? Write its recurrence, attempt the substitution, and show exactly where it breaks.

Recall Solution

Groups of 3: half of medians are , and each such group contributes 2 elements (the median plus one smaller). Guaranteed : . Surviving side . Recurrence: Substitute : . The -terms sum to , giving . To close we'd need , i.e. impossible for . The extra per level never gets absorbed; over levels it stacks to (in fact the pivot guarantee alone doesn't even bound it that tightly). So group-3 is not linear. ∎

L5.2

Adversary / worst-case design. For a 3-way-split Quickselect that always picks the first element as pivot, construct an input of size and a target forcing . Then explain in one line why MoM makes such an adversary impossible.

Recall Solution

Feed an already-sorted array [1,2,3,...,n] and ask for (the maximum). First-element pivot is always the current minimum: partition puts everything into and the pivot lands at rank 1, so we recurse on the remaining elements — one element peeled per call. Work is Why MoM defeats this: the MoM pivot always has elements on each side, so no call can peel off just one element — every recursion discards a constant fraction, capping the depth at and the total at . There is no input that makes MoM's pivot an extreme.

L5.3

Cost accounting. In one MoM call on elements, count the recursive calls and argue why MoM is slower in practice than randomized Quickselect despite both being .

Recall Solution

A single MoM call spawns two recursive Select calls: one on the medians (to find ), and one on the surviving side . Randomized Quickselect spawns one recursive call (into the surviving side only) and pays to pick its pivot. So per level MoM does strictly more work — extra median-finding, extra buffer for medians, a second recursion — inflating the hidden constant (roughly vs. a small expected constant for random). Conclusion: MoM guarantees worst-case but loses on the constant; use it when adversarial or real-time guarantees matter, otherwise prefer randomized Quickselect / introselect.


Recall wrap-up

Recall Quick self-check

Smallest constant for group-5 MoM ::: , giving . Smallest constant for group-7 MoM ::: . Exact count of elements guaranteed for ::: at least (formula lower bound ). Worst-case work of first-pivot Quickselect on sorted input asking for the max ::: . Why group-3 fails, in one phrase ::: fractions sum to , so substitution needs — impossible.

Return to the parent topic · related: Order statistics, Master Theorem, Randomized algorithms, Quicksort.