PCF Learned Sort: a Learning Augmented Sort Algorithm with O(n log log n) Expected Complexity

Atsuki Sato, Yusuke Matsui · OpenReview

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 experimentally faster than classical sorting algorithms, they do not provide theoretical guarantees about its computational complexity. We propose PCF Learned Sort, a theoretically guaranteed Learned Sort algorithm. We prove that the expected complexity of PCF Learned Sort is $O(n \log \log n)$ under mild assumptions on the data distribution. We also confirm experimentally that PCF Learned Sort has a computational complexity of $O(n \log \log n)$ on both synthetic and real datasets. This is the first study to theoretically support the experimental success of Learned Sort, and provides evidence for why Learned Sort is fast.

Introduction

Sorting remains a cornerstone of computational efficiency, with comparison-based algorithms like QuickSort and MergeSort serving as the industry standard for decades. These algorithms are bound by a fundamental lower limit of $O(n \log n)$. In recent years, the emergence of "Learned Sorts" has challenged this status quo. By using machine learning models to estimate the Cumulative Distribution Function (CDF) of input data, these algorithms can predict the approximate position of elements, potentially achieving near-linear sorting times.

However, a significant gap has persisted between empirical performance and theoretical understanding. Most existing learned sorts operate as "black boxes"—they are fast in practice but lack rigorous complexity guarantees. Many fall back to $O(n^2)$ in the worst-case scenarios or simply match $O(n \log n)$ when their internal models fail. PCF Learned Sort addresses this gap by providing the first learned sorting algorithm with a proven expected complexity of $O(n \log \log n)$.

The PCF Learned Sort Algorithm

The core of the proposed method is the Piecewise Constant Function (PCF) Learned Sort. The algorithm utilizes a recursive, model-based bucketing framework designed for both speed and mathematical predictability.

1. CDF Modeling with PCFs

Instead of employing computationally expensive neural networks, the algorithm approximates the data's CDF using a Piecewise Constant Function. It divides the data range into equal-width bins and counts the samples within each. This approach is computationally inexpensive to train and query, ensuring that the overhead of the "learning" component does not negate the gains in sorting speed.

2. Recursive Bucketing and Exception Handling

Using the model's predictions, the input array is partitioned into $\gamma + 1$ buckets. The algorithm then applies a unique "Exception-Handling" mechanism that serves as the foundation for its theoretical guarantees:

  • Success Case: If a bucket is small enough (below a threshold $\delta$), the algorithm recursively applies the learned sort.
  • Failure Case: If a bucket exceeds the threshold $\delta$ (indicating the model failed to distribute the data effectively), the algorithm immediately pivots to a standard comparison-based sort (e.g., QuickSort).
  • Base Case: For very small buckets (below a threshold $\tau$), a standard sort is used to finalize the order.

By carefully tuning parameters ($\alpha, \beta, \gamma, \delta$) to $\lfloor n^{3/4} \rfloor$, the researchers proved that the expected complexity is $O(n \log \log n)$, while the worst-case complexity remains bounded by the internal standard sort ($O(n \log n)$).

Experimental Evaluation

The researchers evaluated PCF Learned Sort against a variety of synthetic and real-world datasets to test its efficiency and robustness.

Datasets

  • Synthetic: Uniform, Normal, Exponential, and Lognormal distributions.
  • Real-World: 16 datasets including Chicago and NYC taxi trip data, Sofia air quality metrics (SOF), Wikipedia edit timestamps, OpenStreetMap (OSM) locations, Amazon book popularity, and NASDAQ stock market data.

Performance and Robustness

Experimental results confirmed the theoretical findings. PCF Learned Sort achieved speeds up to 2.5x faster than C++'s std::sort. When analyzing raw operations relative to $n$, the algorithm's performance remained nearly "flat," empirically validating the $O(n \log \log n)$ behavior.

Crucially, the algorithm demonstrated superior stability against adversarial data. In tests involving "point masses" (large clusters of identical values), other state-of-the-art learned sorts (such as Learned Sort 2.0) experienced catastrophic performance degradation, with runtimes jumping from fractions of a second to over 300 seconds. PCF Learned Sort, thanks to its exception-handling mechanism, maintained performance comparable to std::sort in these scenarios.

Discussion and Future Work

PCF Learned Sort represents a significant milestone in the field of learning-augmented algorithms. By achieving $O(n \log \log n)$ expected complexity, it provides a mathematical explanation for why learned sorts are effective. For a dataset of one billion elements, $\log n$ is approximately 30, whereas $\log \log n$ is roughly 5, representing a massive theoretical and practical improvement.

While the current implementation is highly effective, there are avenues for further optimization. The algorithm currently requires auxiliary memory for bucketing, meaning it is not "in-place." Additionally, while the $O(n \log \log n)$ guarantee is robust, it assumes a bounded probability density function. Future research may focus on integrating these theoretical principles into cache-aware or parallelized frameworks to further enhance performance on modern hardware.

Summary updated

BibTeX
@misc{sato2024pcf,
  author = {Atsuki Sato and Yusuke Matsui},
  title = {{PCF Learned Sort: a Learning Augmented Sort Algorithm with O(n log log n) Expected Complexity}},
  year = {2024},
  eprint = {2405.07122},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.07122},
}

Loading related content...

Figure