Recursively partition into equal halves by rank, keeping the selected median in the lower half:
quantiles(A,k):
    if length(A) == k:
        return [maximum(A)]
    p = select the (length(A)/2)-th smallest element of A
    L = all elements of A at most p
    R = all elements of A greater than p
    return quantiles(L,k) followed by quantiles(R,k)
All elements are distinct, so both halves have exactly elements. Since remains a power of two, every internal split occurs at a rank divisible by .
For correctness, the desired ranks in the lower half are . In the upper half they are the same local multiples of , shifted by . These are exactly all desired global ranks. At a leaf of size , its maximum is its sole requested local rank. Induction therefore proves that the output contains every requested order statistic, in increasing order, including the original maximum. There is no separate internal-node output of p, which would duplicate a rank already supplied by the lower subtree.
Use a worst-case linear-time selection algorithm and linear partitioning. The divide-and-conquer algorithm has
There are internal levels, each with total work , and leaves with total work . Since the source assumes , the running time is
This is multiselection by balanced rank partitioning. In-place selection and partitioning can use the original array; the output has values. The multiselection recursion has depth ; the selection routine contributes its own auxiliary storage.
Quicksort 2026-10-24
A divide-and-conquer algorithm that partitions entries relative to a pivot, recursively sorts the smaller and larger partitions, and combines their results. A three-way partition keeps all equal keys in a central bucket. Correctness follows from the partition order and recursive correctness; excluding the pivot from child inputs provides decreasing finite lengths. The choice of pivot controls balance and therefore running time.
The minimum and maximum of a nonempty array can be found with at most pairwise comparisons. Compare consecutive pairs once, retaining smaller entries as minimum candidates and larger entries as maximum candidates. If an entry is unpaired, put it into both candidate sets. Each candidate set has entries, and finding one extremum on entries uses comparisons. Thus the total is , equal to the stated bound; the singleton case uses zero. Balanced divide-and-conquer algorithms can perform both candidate reductions. Pairing is important: arbitrary equal splitting with only a two-element base case does not attain this comparison count at every size.