Apex › Storage & databases

Median of medians — worst-case linear selection

How to find the k-th smallest in guaranteed O(n), even in the worst case, by choosing a provably good pivot. Split into groups of 5, take each group's median, then recursively take the median of those medians — a pivot that can never land in the extreme tails, so every partition is balanced. Watch the groups, their medians, the pivot, and the guaranteed balance.

More in Storage & databases