[Paper Review] Quicksort with median of medians is considered practical
This paper proposes a practical variant of quicksort using median-of-medians pivot selection with input thinning, demonstrating that with optimal thinning parameters (e.g., s=40), the algorithm achieves O(n log n) worst-case performance while matching or outperforming standard optimized quicksorts on random data. The method reduces the constant factor of median-of-medians through selective sampling, making it both theoretically robust and empirically efficient.
The linear pivot selection algorithm, known as median-of-medians, makes the worst case complexity of quicksort be $\mathrm{O}(n\ln n)$. Nevertheless, it has often been said that this algorithm is too expensive to use in quicksort. In this article, we show that we can make the quicksort with this kind of pivot selection approach be efficient.
Motivation & Objective
- To challenge the long-standing belief that median-of-medians pivot selection is too slow for practical use in quicksort.
- To improve the constant factor of median-of-medians by thinning the input before pivot selection, reducing computational overhead.
- To demonstrate that the resulting algorithm maintains O(n log n) worst-case complexity while achieving competitive performance on average-case inputs.
- To extend the thinning technique to other pivot selection strategies, such as pseudo-median of 3^L, and evaluate their performance.
- To provide empirical evidence that the modified algorithm outperforms standard methods like median-of-three and pseudo-median-of-nine on random sequences.
Proposed method
- Introduces a thinning strategy where only every s-th element from the input array is used for median-of-medians pivot selection, reducing the input size to n/s.
- Applies the BFPRT algorithm on the thinned subset to select a pivot that guarantees at least 30% of elements are on each side, ensuring O(n log n) worst-case complexity.
- Derives a recurrence relation to estimate the asymptotic constant factor a in the time complexity an log n + bn, showing it decreases with increasing s.
- Proposes an alternative method using pseudo-median of 3^L with thinning, where the array is recursively partitioned into three parts and the median of medians of subparts is selected.
- Uses least squares fitting on experimental comparison counts to estimate coefficients a and b in the model an log n + bn for various pivot strategies.
- Employs a single-pivot partitioning scheme and the original BFPRT algorithm, without switching to insertion sort for small subarrays, to isolate the effect of pivot selection.
Experimental results
Research questions
- RQ1Can median-of-medians pivot selection in quicksort be made practical by reducing the input size through thinning?
- RQ2What is the optimal thinning parameter s that minimizes the constant factor in the worst-case time complexity while maintaining good average-case performance?
- RQ3How does the performance of the thinned median-of-medians method compare to established methods like median-of-three and pseudo-median-of-nine on random sequences?
- RQ4Can the thinning technique be generalized to other pivot selection strategies, such as pseudo-median of 3^L, to improve their worst-case behavior?
- RQ5Does the empirical performance of the thinned median-of-medians algorithm align with theoretical predictions regarding comparison counts and asymptotic complexity?
Key findings
- With s=40, the thinned BFPRT method (t-BFPRT) achieves a coefficient a ≈ 1.5371 in the comparison count model an log n + bn, outperforming median-of-three (a ≈ 1.710) and approaching pseudo-median-of-nine (a ≈ 1.568).
- The t-BFPRT method with s=40 shows comparable performance to the well-optimized pseudo-median-of-nine (PMed9) method, with nearly identical comparison counts.
- For the t-PMed3^L method, s=40 yields a coefficient a ≈ 1.528, which is slightly better than t-BFPRT with s=40 and significantly better than median-of-three.
- The coefficient a for t-BFPRT decreases monotonically with increasing s, approaching the theoretical lower bound of 1/log 2 ≈ 1.443 as s increases.
- The performance of t-BFPRT and t-PMed3^L with s=40 is robust on random sequences, with low variance and strong empirical fit to the an log n + bn model.
- The method remains efficient even in the worst case, with guaranteed O(n log n) comparison count, while maintaining competitive performance on average inputs.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.