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:
- Allocates a zero-initialized count array
cntof lengthnumKeysviaMemory.allocBlock. - Allocates a dirty output record array
outof lengthstride * countviaMemory.allocBlockDirty. - Counts bucket sizes in
cnt, computes exclusive prefix offsets viaArrayOps.prefixSumProg, and scatters each input record to its bucket offset usingMemory.copy.
Main definitions #
countingSortPure: pure stable bucket-filter model over keys0 .. numKeys - 1.CountingSortArgs,countingSortArgsWords: explicit 5-word arguments and encoding.countingSortProg: 3-passMemory.Prog Blockreturning the freshly allocated sorted block.
Main results #
countingSortPure_perm,countingSortPure_pairwise_key,countingSortPure_stable: permutation, key monotonicity, and equal-key stability ofcountingSortPure.countingSortProg_spec:Memory.FramedTriplewithS = ∅(all pre-existing memory preserved), exact linear memory cost2 * (stride * rows.length) + 6 * rows.length + 3 * numKeys + 2,Memory.WellFormed, andHoldsRecords stride out.base (countingSortPure ...).realizes_countingSortProg:Realizes .exactcertificate withwork = 0.
Pure model and stability properties #
When all keys lie below numKeys, countingSortPure permutes xs.
Stability: countingSortPure preserves any pairwise relation R within equal-key buckets.
countingSortPure produces elements ordered monotonically by keyOf.
Arguments and Memory.Prog implementation #
Input parameters for stable linear-time counting sort over fixed-stride records.
Explicit 5-word encoding of CountingSortArgs.
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 #
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.
Uniform Realizes .exact certificate for countingSortProg with work = 0.