Stable counting-sort positions #
Main definitions #
CountingSort.position: stable scatter position of an original occurrence.
Main results #
CountingSort.count_lt_succ: the prefix-sum update used by a native counting pass.CountingSort.position_injective: every original position has a distinct destination.CountingSort.position_bijective: scatter destinations form a permutation of the input.CountingSort.scatter_span_image: an actual scattered array's bucket span has exactly the values whose original keys select that bucket.
These finite counting identities are original supporting mathematics for the native router.
They reuse Mathlib's Nat.count and its prefix-count APIs directly; only the stable-scatter
construction and cross-bucket theory are added. They assert no program or execution cost.
Each occurrence lands before the next bucket, and after its own bucket's start.
theorem Cslib.Algorithms.CountingSort.position_injective (key : ℕ → ℕ) (n : ℕ) :
Function.Injective fun (i : Fin n) => position key n ↑i
Distinct original positions never overwrite one another during stable scatter.
theorem Cslib.Algorithms.CountingSort.position_bijective (key : ℕ → ℕ) (n : ℕ) :
Function.Bijective fun (i : Fin n) => ⟨position key n ↑i, ⋯⟩
All original occurrences, including equal keys, are a permutation of the output cells.
theorem Cslib.Algorithms.CountingSort.scatter_span_image {α : Type u_1} [DecidableEq α] (key : ℕ → ℕ) (n b : ℕ) (stored values : ℕ → α) (h : ∀ i < n, stored (position key n i) = values i) :
Finset.image stored (Finset.Ico (Nat.count (fun (k : ℕ) => key k < b) n) (Nat.count (fun (k : ℕ) => key k < b + 1) n)) = Finset.image values ({i ∈ Finset.range n | key i = b})
A memory array satisfying the stable-scatter equations has precisely the original values selected by each bucket. No sorted list or permutation certificate is a premise.