Documentation

CslibFrontier.Algorithms.WordRAM.CountingSort

Stable Linear-Time Counting Sort over Fixed-Stride Records #

This module implements and verifies a general stable linear-time counting sort over fixed-stride record arrays in Memory.Prog (countingSortProg), together with the pure list model countingSortPure.

Given count contiguous records of width stride starting at inBase whose keys at word offset keyOff < stride lie in [0, numKeys), countingSortProg:

  1. Allocates a zero-initialized count array cnt of length numKeys via Memory.allocBlock.
  2. Allocates a dirty output record array out of length stride * count via Memory.allocBlockDirty.
  3. Counts bucket sizes in cnt, computes exclusive prefix offsets via ArrayOps.prefixSumProg, and scatters each input record to its bucket offset using Memory.copy.

Main definitions #

Main results #

Pure model and stability properties #

def Cslib.Algorithms.WordRAM.countingSortPure {α : Type u_1} (numKeys : ℕ) (keyOf : α → ℕ) (xs : List α) :
List α

Pure reference model of stable counting sort by keyOf : α → ℕ over keys 0 .. numKeys - 1.

theorem Cslib.Algorithms.WordRAM.countingSortPure_perm {α : Type u_1} (numKeys : ℕ) (keyOf : α → ℕ) (xs : List α) (hbound : ∀ x ∈ xs, keyOf x < numKeys) :
(countingSortPure numKeys keyOf xs).Perm xs

When all keys lie below numKeys, countingSortPure permutes xs.

theorem Cslib.Algorithms.WordRAM.countingSortPure_stable {α : Type u_1} (numKeys : ℕ) (keyOf : α → ℕ) {R : α → α → Prop} {xs : List α} (hR : List.Pairwise R xs) :
List.Pairwise (fun (x y : α) => keyOf x < keyOf y ∨ keyOf x = keyOf y ∧ R x y) (countingSortPure numKeys keyOf xs)

Stability: countingSortPure preserves any pairwise relation R within equal-key buckets.

theorem Cslib.Algorithms.WordRAM.countingSortPure_pairwise_key {α : Type u_1} (numKeys : ℕ) (keyOf : α → ℕ) (xs : List α) :
List.Pairwise (fun (x y : α) => keyOf x ≤ keyOf y) (countingSortPure numKeys keyOf xs)

countingSortPure produces elements ordered monotonically by keyOf.

Arguments and Memory.Prog implementation #

Input parameters for stable linear-time counting sort over fixed-stride records.

  • numKeys : ℕ

    Exclusive upper bound on record keys (0 ≤ key < numKeys).

  • count : ℕ

    Number of records in the input array.

  • stride : ℕ

    Number of machine words per record (keyOff < stride).

  • keyOff : ℕ

    Zero-based word offset of the sort key inside each record.

  • inBase : ℕ

    Base address of the contiguous input record array.

theorem Cslib.Algorithms.WordRAM.countingSortArgsWords_fits {w : ℕ} {a : CountingSortArgs} (h_numKeys : WordRAM.Fits w a.numKeys) (h_count : WordRAM.Fits w a.count) (h_stride : WordRAM.Fits w a.stride) (h_keyOff : WordRAM.Fits w a.keyOff) (h_inBase : WordRAM.Fits w a.inBase) (j✝ : Fin 5) :
@[reducible, inline]

The tuple word encoding of the fields of countingSortArgsWords.

Sort a.count contiguous fixed-stride records at a.inBase (each of width a.stride, with sort key < a.numKeys at word offset a.keyOff) stably into a freshly allocated block using three linear passes: histogram count, ArrayOps.prefixSumProg, and Memory.copy scatter.

Multi-pass verification #

theorem Cslib.Algorithms.WordRAM.countingSortProg_spec {w numKeys stride keyOff inBase : ℕ} {rows : List (Fin stride → ℕ)} {s : DataStructure.Memory.State} (hwf : DataStructure.Memory.WellFormed w s) (hxs : WordRAM.Realization.HoldsRecords stride inBase rows s.mem) (hin : inBase + stride * rows.length ≤ s.frontier) (hkey : keyOff < stride) (hbound : ∀ r ∈ rows, r ⟨keyOff, hkey⟩ < numKeys) (hfit : WordRAM.Fits w (s.frontier + numKeys + stride * rows.length)) :
DataStructure.Memory.FramedTriple w s (countingSortProg { numKeys := numKeys, count := rows.length, stride := stride, keyOff := keyOff, inBase := inBase }) (2 * (stride * rows.length) + 6 * rows.length + 3 * numKeys + 2) ∅ fun (out : WordRAM.Block) (s' : DataStructure.Memory.State) => out = { base := s.frontier + numKeys, length := stride * rows.length } ∧ out.Allocated s' ∧ s'.frontier = s.frontier + numKeys + stride * rows.length ∧ DataStructure.Memory.WellFormed w s' ∧ WordRAM.Realization.HoldsRecords stride out.base (countingSortPure numKeys (fun (r : Fin stride → ℕ) => r ⟨keyOff, hkey⟩) rows) s'.mem

Framed specification for countingSortProg: stably sorts rows by key field keyOff into a freshly allocated block out = ⟨s.frontier + numKeys, stride * rows.length⟩, preserves all cells below s.frontier (S = ∅), preserves WellFormed, and costs at most 2 * (stride * rows.length) + 6 * rows.length + 3 * numKeys + 2.