Abstract
Sorting is one of the most fundamental algorithms in computer science. Recently, Learned Sorts, which use machine learning to improve sorting speed, have attracted attention. While existing studies show that Learned Sort is empirically faster than classical sorting algorithms, they do not provide theoretical guarantees about its computational complexity. We propose Piecewise Constant Function (PCF) Learned Sort, a theoretically guaranteed Learned Sort algorithm. We prove that the expected complexity of PCF Learned Sort is $\mathcal{O}(n \log \log n)$ under mild assumptions on the data distribution. We also confirm empirically that PCF Learned Sort has a computational complexity of $\mathcal{O}(n \log \log n)$ on both synthetic and real datasets. This is the first study to theoretically support the empirical success of Learned Sort, and provides evidence for why Learned Sort is fast.
Introduction
Sorting is a cornerstone of computer science, yet performance improvements in comparison-based algorithms have largely plateaued, constrained by the mathematical lower bound of $\Omega(n \log n)$. Recently, "Learned Sorts" have emerged as a promising alternative. These algorithms use machine learning to estimate the Cumulative Distribution Function (CDF) of input data. By predicting the final position of elements, these algorithms can potentially achieve $\mathcal{O}(n)$ performance.
However, a significant challenge remains: while learned sorts are fast in practice, they often lack theoretical safety nets. Poor model predictions—often caused by duplicates or unusual data distributions—can cause complexity to spiral to $\mathcal{O}(n^2)$, making them risky for general-purpose applications. This paper introduces PCF Learned Sort, an algorithm that is both empirically fast and theoretically "safe," providing a rigorous guarantee of $\mathcal{O}(n \log \log n)$ expected complexity.
Methodology
The authors introduce the Piecewise Constant Function (PCF) Learned Sort, which follows a recursive, model-based bucketing framework.
CDF Estimation via PCF
The algorithm samples a small portion of the data to train a PCF model. This model divides the data range into equal-width bins and counts how many samples fall into each, creating a "staircase" approximation of the CDF. This simple model ensures that training and inference overhead is minimal.
Recursive Bucketing and the Safety Net
Using the model's predictions, the array is partitioned into $\gamma + 1$ buckets. The key innovation is the "Safety Net" for exception handling:
- Recursive Step: If a bucket is small enough (below a threshold $\tau$), it is sorted using a standard algorithm.
- Fallback Mechanism: If a bucket is "too large" (exceeding a threshold $\delta$), the algorithm identifies a bucketing failure and immediately falls back to a Standard Sort (e.g., IntroSort).
By carefully choosing the number of bins and the failure threshold, the authors prove an expected complexity of $\mathcal{O}(n \log \log n)$ and a worst-case complexity that matches the fallback algorithm.
Experimental Evaluation
The algorithm was evaluated on a massive scale using both synthetic and real-world data:
- Synthetic: Uniform, Normal, Exponential, and Lognormal distributions.
- Real-World: 16 datasets including transportation (Chicago/NYC taxi data), environment (Sofia air quality), web/social (Wikipedia, OSM, Facebook), and finance (NASDAQ).
- Adversarial: Datasets with massive amounts of duplicate values were used to test robustness against model failure.
Key Results
- Speed: PCF Learned Sort is consistently faster than
std::sort, achieving up to a 2.5x speedup. - Efficiency: For $n = 10^9$, $\log n \approx 30$, while $\log \log n \approx 5$. This asymptotic advantage makes the algorithm significantly faster for large-scale data.
- Reliability: In adversarial tests where other learned sorts took over 300 seconds, PCF Learned Sort finished in just 0.45 seconds, demonstrating the effectiveness of its fallback mechanism.
- Theoretical Validation: Empirical results closely matched the predicted $\mathcal{O}(n \log \log n)$ curve, confirming the practical validity of the theoretical proof.
Conclusion and Future Work
PCF Learned Sort provides the first theoretical evidence for why learned sorts are fast and how they can be made robust. While currently not "in-place" (requiring auxiliary memory), future work could explore in-place variants and low-level hardware optimizations to further improve performance on modern CPUs.
Summary updated