← All papers
First page of Spectral Contraction for Shellsort with Restricted Gaps

Spectral Contraction for Shellsort with Restricted Gaps

Miaozhen Yu

cs.DS Dec 2, 2026 · v1
A Lean 4 development machine-checks the Θ(n log n) optimal mean comparison order for Shellsort, the parity lower bound, and the periodic-menu lemmas.
For every eventually periodic set of permitted nonfinal gaps, we determine the order of the minimum mean number of Shellsort comparisons. We also determine its leading coefficient in several cases. The gap table is fixed before the input is drawn uniformly and ends in gap $1$. For a nonempty tail of period $q$, let $d$ be the gcd of $q$ and its allowed residues. The optimal mean is $Θ(n\log n)$ for $d=1$ and $Θ(n^{3/2})$ for $d>1$. For $d=2$ the largest allowed odd gap $h$, including $1$, gives the exact coefficient $\sqrt{π/128}/h$. For a finite catalogue, let $B$ be the largest gap, counting the compulsory gap $1$. The minimum mean is $n^2/(4B)+O_B(n^{3/2})$. A spectral estimate for a word of chain sorts yields schedules on the residue classes. An inversion transfer lemma gives the cost of each divisor change. A matching lower bound gives the exact coefficient for nested divisor exceptions. Sharpness of the upper bound for general mixed exceptions remains open. When $d=1$, prefixes of one random schedule give approximate rankings at every prescribed accuracy. The unrestricted mean order also follows from concurrent work of Du and Wan. Lean verifies this order and the pointwise parity lower bound.

The paper determines the order, and in several cases the exact leading coefficient, of the minimum mean number of Shellsort comparisons on uniform inputs. The gap table is fixed in advance and the permitted nonfinal gaps form an eventually periodic set.

A spectral contraction estimate for a word of chain sorts uses cut statistics and Fourier analysis. It yields multiscale gap schedules on residue classes. An inversion transfer lemma charges the cost of each divisor change, and matching lower bounds give exact coefficients for nested divisor exceptions. A Lean 4 development models chain insertion on slot arrays and checks the menu lemmas, scale budgets, the exceptional-gap lower bound, and the n log n optimum.

The optimal mean is Θ(n log n) when d=1 and Θ(n^{3/2}) when d>1. The coefficient is exact for d=2 (√(π/128)/h) and n²/(4B) for finite catalogues. Lean verifies log₂(n!) ≤ m_n ≤ (1+2^165) n log₂ n and the pointwise parity lower bound.

Permitted gapsm_n(A)
d=1Θ(n log n)
d=2, largest odd gap h(c_2/h) n^{3/2} + O(n log n)
d≥3, d divides all gapsc_d n^{3/2} + O(n log n)
d≥3, some gap not divisible by dΘ(n^{3/2})
R=∅, largest gap Bn²/(4B) + O_B(n^{3/2})
Minimum mean comparisons by permitted-gap class (Theorem 1.1)