NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Faster Shellsort with Reinforcement Learning

Qingyun Li, Yunjin Li, Yunqi Li, Ruoyun LiNeurIPS 2026

TL;DR

For over sixty years, Shellsort has relied on fixed, one-size-fits-all "gap sequences" to sort arrays, blind to what the data actually looks like. By teaching a lightweight reinforcement learning policy to "read" an array's multi-scale disorder, the authors dynamically tailor gap sequences to the input—cutting element comparisons by 13–16% over classic baselines. Better yet, for predictable workloads, these learned schedules can be compiled offline into static, zero-overhead C-style arrays.

What Problem Does This Paper Address?

Ordinary insertion sort is painfully slow when a small element is stranded at the wrong end of an array, inching home one step at a time. Invented in 1959, Shellsort fixes this by running multiple insertion-sort passes over elements separated by a decreasing sequence of "gaps" ($h_1 > h_2 > \dots > 1$). Large early gaps launch elements across the array to fix macro-level chaos; smaller later gaps clean up local neighborhoods. Because it is simple, in-place, and requires virtually zero extra memory, Shellsort still powers embedded systems everywhere—from uClibc-ng in embedded Linux to the U-Boot bootloader on billions of ARM devices.

The catch? Finding the optimal gap sequence has been an open problem for six decades. Legendary sequences like Knuth, Sedgewick, and Ciura are static: once you know the array size $N$, every input gets the exact same sequence of gaps. If an array is already nearly sorted, or structured like a sawtooth wave, classical Shellsort wastes passes checking length scales where no disorder actually exists. The authors ask: what if the gap sequence adapted to where the disorder actually lies?

Methods

The core novelty here is framing gap selection as an episodic Markov Decision Process (MDP) guided by a compact fingerprint of array chaos called the disorder spectrum.

  • The Disorder Spectrum (Novel): Because Shellsort gaps operate at different length scales, the authors measure disorder at logarithmically spaced strides ($s \in \{1, 2, 4, \dots, N/2\}$). At each stride, they calculate the fraction of element pairs $(a_i, a_{i+s})$ that are out of order ("inverted"). Combined with the remaining pass budget and the previous gap size, this gives a tiny state vector that grows only logarithmically with $N$.
  • RL Policy (Standard): A tiny two-layer MLP observes this state and picks the next strictly smaller gap from a discrete candidate set. It is trained via Proximal Policy Optimization (PPO) to minimize the number of comparisons made during each pass, plus a tiny step penalty to discourage needlessly long schedules. After each pass, the array updates in place, and the spectrum is recomputed.
  • Offline Compilation (Novel): Running a neural net inside a sorting loop will destroy wall-clock speed. For applications with stable data distributions, the authors run the trained policy offline on sample data, validate the best static sequence it discovers, and hard-code those integers directly into standard Shellsort—eliminating all neural and spectrum overhead at runtime.

Datasets

Rather than using external static benchmarks, the authors train and evaluate on synthetic arrays ($N=128$ to $N=16,384$) sampled uniformly across seven diverse input families:

  1. Random permutations
  2. Nearly-sorted arrays
  3. Reversed arrays with local noise
  4. Interleaved blocks
  5. Pipe-organ arrays (ascending then descending)
  6. Sawtooth arrays (periodic ramps)
  7. Arrays with many duplicate values

Key Insights and Ideas

  • Classical sequences leave huge headroom on structured data: On pure random noise, Ciura's sequence (empirically tuned for random permutations) essentially ties with the RL policy. The magic happens on structured data—like nearly-sorted or periodic arrays—where the policy realizes it can skip massive strides and jump straight to fine-grained cleanup.
  • Most of the signal is in the starting lineup: Ablations show that just looking at the initial disorder spectrum before sorting begins captures the vast majority of the performance gain (jumping from ~4–6% improvement with no observation to ~13–14%). Recomputing the spectrum after every pass squeezes out the final 0.6 to 2.5 percentage points, proving sequential adaptation matters more as arrays get larger.
  • You don't need a high-res spectrum: Measuring inversions at just four logarithmically spaced strides recovers almost all the performance of the full power-of-two spectrum.
  • Schedules mirror physics-like length scales: When compiled offline, nearly-sorted and sawtooth workloads (local disorder) produce short, tight schedules like [4, 1] and [7, 1], whereas reversed and pipe-organ arrays (global disorder) trigger large, sweeping initial gaps.

Key Results

Training convergence and per-family scaling from N=128 to 16,384

Figure 1: Scaling from N = 128 to N = 16,384. (a) Training curves for the policies trained at each size. Despite the candidate action space growing from 27 to 74 gaps, all eight policies converge within roughly 150–250 PPO updates. (b) The advantage over Sedgewick remains structured by input family as N grows. Families that benefit strongly from adaptation continue to do so at larger sizes, while random and many-duplicate inputs remain much closer to the classical baseline.
  • Consistent comparison reductions: Across array sizes from $N=128$ to $16,384$, the live adaptive policy reduces total comparisons by 13.5% to 15.9% relative to Sedgewick (and up to 20.6% against Knuth at $N=16,384$).
  • Massive wins on structured inputs: At $N=256$, the policy beats all seven classical baselines on every family, slashing comparisons over Sedgewick by 33.9% on nearly-sorted arrays and 28.7% on sawtooth arrays.
Table 1: Per-family results at array size N = 256 (mean ± std over 30 training seeds). Lower is better. RD = random, NS = nearly sorted, RV = reversed, IL = interleaved, PO = pipe organ, ST = sawtooth, and MD = many duplicates. The best per-column value is bolded, and the second-best is underlined.
  • Fast, accessible training: Because the MDP operates at a high abstraction level (choosing gaps rather than assembly instructions like DeepMind's AlphaDev), training 500 PPO updates takes just 66 seconds at $N=128$ and 7.4 minutes at $N=16,384$ on a single CPU.
  • Zero-overhead compilation works: Hard-coding workload-specific compiled sequences retains 90.5% to 95.6% of the live policy's gains over Sedgewick while running as an ordinary, static Shellsort.

Limitations and Future Work

  • Runtime overhead of the "live" policy: Computing the disorder spectrum and running MLP inference after every pass adds substantial wall-clock overhead. Live mode is primarily a theoretical upper bound and discovery tool, not a drop-in runtime replacement.
  • Distribution shift: Compiled static schedules assume your target workload has recurring structure represented during training. If a compiled schedule tuned for nearly-sorted data suddenly encounters globally reversed arrays, performance may degrade. Furthermore, policies are not assumed to generalize zero-shot to arbitrary unseen structures.
  • Future directions: Explicit future research directions are not discussed in the paper, though the authors frame this work broadly as a template for using RL to tune hyperparameters of classical algorithms for specialized workloads.

Summary updated

BibTeX
@inproceedings{li2026faster,
  author = {Qingyun Li and Yunjin Li and Yunqi Li and Ruoyun Li},
  title = {{Faster Shellsort with Reinforcement Learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=Ym8pFbqtzJ},
}
Figure