Apex › Storage & databases

Quickselect — the k-th smallest in O(n)

Find a median or a percentile without sorting. Quickselect partitions around a pivot that lands at its final sorted position, then keeps only the side containing k and repeats — discarding half each time for O(n) average. Pick which order statistic you want and step the partitions as the search window collapses onto the answer.

More in Storage & databases