Documentation

CslibFrontier.Algorithms.WordRAM.MergeSort

Stable Key-Indexed Bottom-Up Merge Sort on the Word RAM #

This module implements deterministic O(n log n) bottom-up iterative key-indexed permutation merge sort (mergeSortPerm) over Cslib.WordRAM.Basis.standard. Given an n-word array keys : List ℕ at keyBase, an n-word output slice at permBase, and an n-word scratch buffer at bufBase (keyBase + n ≤ permBase and permBase + n ≤ bufBase), mergeSortPerm initializes perm[0..n) := [0, …, n - 1] and performs ⌈log₂ n⌉ bottom-up merge passes using Cslib.WordRAM.ArrayCopy.program for each pass's copy-back step.

Because equal keys break ties toward the left run, mergeSortPerm preserves the strict asymmetric order StableKeyOrder keys, so indexing any list xs by the resulting permutation perm agrees propositionally (=) with Lean's stable List.mergeSort (filterMap_getElem?_eq_mergeSort).

Main definitions #

Main results #

References #

Strict tie-breaking key order on 0 .. keys.length - 1: orders indices by strictly increasing keys.getD · 0, breaking ties by smaller original index.

theorem Cslib.Algorithms.WordRAM.MergeSort.StableKeyOrder.le {keys : List ℕ} {a b : ℕ} (h : StableKeyOrder keys a b) :
keys.getD a 0 ≤ keys.getD b 0

Pure Stability Equivalence with List.mergeSort #

theorem Cslib.Algorithms.WordRAM.MergeSort.filterMap_getElem?_eq_mergeSort {α : Type u_1} (xs : List α) (key : α → ℕ) {perm : List ℕ} (hperm : perm.Perm (List.range xs.length)) (hpw : List.Pairwise (StableKeyOrder (List.map key xs)) perm) :
List.filterMap (fun (x : ℕ) => xs[x]?) perm = xs.mergeSort fun (x y : α) => decide (key x ≤ key y)

Indexing xs : List α by any permutation perm of List.range xs.length ordered by StableKeyOrder (xs.map key) yields xs.mergeSort (fun x y => decide (key x ≤ key y)) identically.

Private Slice & Memory Helpers #

Registers (Fin 18) and Structured Program #

@[reducible, inline]

Number of elements n = keys.length to sort.

@[reducible, inline]

Base address kb of the n-word key array keys.

@[reducible, inline]

Base address pb of the n-word output index permutation perm.

@[reducible, inline]

Base address bb of the n-word merge scratch buffer.

Deterministic bottom-up iterative key-indexed permutation merge sort over Basis.standard.

Loop Invariants and Execution Proof #

theorem Cslib.Algorithms.WordRAM.MergeSort.mergeSortPerm_exec {w kb pb bb : ℕ} (keys : List ℕ) (x : WordRAM.Data 18) (hn : x.regs Reg.len = keys.length) (hkb : x.regs Reg.keyBase = kb) (hpb : x.regs Reg.permBase = pb) (hbb : x.regs Reg.bufBase = bb) (hkeys : WordRAM.Block.HoldsList kb keys x.mem) (hdisj1 : kb + keys.length ≤ pb) (hdisj2 : pb + keys.length ≤ bb) (hfit : WordRAM.Fits w (bb + 2 * keys.length + 1)) :
∃ (y : WordRAM.Data 18), WordRAM.ExecWithin w mergeSortPerm x y (52 * keys.length * Nat.clog 2 keys.length + 5 * keys.length + 7) ∧ ∃ (perm : List ℕ), WordRAM.Modifies { base := pb, length := bb + keys.length - pb }.footprint x.mem y.mem ∧ WordRAM.Block.HoldsList pb perm y.mem ∧ perm.Perm (List.range keys.length) ∧ List.Pairwise (StableKeyOrder keys) perm ∧ List.Pairwise (fun (a b : ℕ) => keys.getD a 0 ≤ keys.getD b 0) perm ∧ y.regs Reg.len = keys.length ∧ y.regs Reg.keyBase = kb ∧ y.regs Reg.permBase = pb ∧ y.regs Reg.bufBase = bb

O(n log n) Word-RAM Stable Key-Indexed Permutation Merge Sort: given keys : List ℕ at kb and disjoint n-word output and scratch blocks at pb and bb (kb + n ≤ pb and pb + n ≤ bb), mergeSortPerm halts within 52 * n * Nat.clog 2 n + 5 * n + 7 instructions and writes a permutation perm of List.range n at pb sorted under both StableKeyOrder keys and fun a b => keys.getD a 0 ≤ keys.getD b 0.