In-place randomized quickselect on the word RAM #
Iterative in-place 3-way randomized quickselect over Cslib.WordRAM.Basis.standard. Given an
n-word slice starting at address base and a 0-indexed target rank k < n, the algorithm
selects the k-th smallest element input.mergeSort[k] in place using zero auxiliary memory
words (O(1) registers only).
The selection loop maintains a single active subarray [lo, hi) of length m = hi - lo
containing the target address target = base + k (lo ≤ target < hi). When 2 ≤ m, each
iteration samples a uniform pivot offset off ← rand (m - 1) with pivot value
piv = mem (lo + off) and applies Dutch National Flag 3-way partitioning to split [lo, hi) into
< piv on [lo, lt), = piv on [lt, gt), and > piv on [gt, hi):
- Pathwise correctness and zero auxiliary space: Every execution path halts cleanly and
returns
input.mergeSort[k]. The final memory permutes[base, base + n)in place so thatmem (base + k) = input.mergeSort[k],mem (base + i) ≤ mem (base + k)fori < k, andmem (base + k) ≤ mem (base + i)fork < i < n. All memory outsideFinset.Ico base (base + n)is unchanged. - Strict worst-case bounds: Because the
= pivregion[lt, gt)contains the sampled pivot,lt < gtholds and each iteration strictly decreasesm = hi - lo. Every execution path makes at mostn - 1randcalls and executes at most10 * n ^ 2 + 15instructions. - Unconditional linear expected running time (including duplicates): All copies of
pivin[lo, hi)land in[lt, gt). Whenlt ≤ target < gt, the active range collapses immediately to length1; otherwise, for a sampled pivot of sorted rankr < min[lo, hi), the next active subarray has length at mostmax r (m - 1 - r). The identity4 * ∑_{r < m} max r (m - 1 - r) ≤ 3 * m ^ 2gives a(3 / 4)-shrinking linear potential80 * mon the active subarray lengthm, provingexpectedTime w program (.init (k :: input)) ≤ 80 * n + 15uniformly over allk < nand all duplicate-key patterns. - Total out-of-range handling: On
State.init (k :: input)satisfyingInputFits w (k :: input)withn ≤ k,programskipsselectSliceand halts deterministically in8instructions with output0,0randcalls, and unchanged memory.
Main definitions #
Cslib.Algorithms.WordRAM.Quickselect.samplePivot,Cslib.Algorithms.WordRAM.Quickselect.narrowRange,Cslib.Algorithms.WordRAM.Quickselect.partitionAndNarrow,Cslib.Algorithms.WordRAM.Quickselect.selectStep,Cslib.Algorithms.WordRAM.Quickselect.selectSetup,Cslib.Algorithms.WordRAM.Quickselect.loadHeader: structured building blocks of the selection loop andData.initheader.Cslib.Algorithms.WordRAM.Quickselect.selectSlice: reusable 16-register structured subroutine selecting rankregs rkin[regs base, regs base + regs len)in place.Cslib.Algorithms.WordRAM.Quickselect.code,Cslib.Algorithms.WordRAM.Quickselect.program: structured and compiled 16-register randomized quickselect onState.init (k :: input).Cslib.Algorithms.WordRAM.Quickselect.worstCaseTime: quadratic worst-case step bound10 * n ^ 2 + 15.Cslib.Algorithms.WordRAM.Quickselect.expectedTimeBound: linear expected step bound80 * n + 15.
Main results #
Cslib.Algorithms.WordRAM.Quickselect.selectSlice_spec:ExecRandWithinand linearexpectedCodeTimespecification forselectSliceon an arbitrary slice[base, base + n).Cslib.Algorithms.WordRAM.Quickselect.loadHeader_spec,Cslib.Algorithms.WordRAM.Quickselect.code_spec,Cslib.Algorithms.WordRAM.Quickselect.code_out_of_range_spec: structuredloadHeaderandcodespecifications fork < input.lengthandinput.length ≤ k.Cslib.Algorithms.WordRAM.Quickselect.quickselect_correct_and_bounds: joint certificate ofRandHaltsWithin,RandCallsBounded(≤ n - 1), pathwise order-statistic output, in-place partition order, zero-auxiliary-memoryModifiesframe, andO(n)expectedTimeat word sizew ≥ logWidth 2 input.length.Cslib.Algorithms.WordRAM.Quickselect.quickselect_out_of_range: deterministic8-step halting wheninput.length ≤ k.
Classical randomized selection (RANDOMIZED-SELECT) appears in [CLRS09], Section 9.2; see also
[MU17], Section 3.5 (sampling-based median selection, related background). The
3-way Dutch National Flag word-RAM formulation, zero-auxiliary-memory frame, pure-ℕ linear drift
potential 80 * m, and duplicate-key rank domination are original to this formalization.
References #
Expected-selection linear potential recurrence #
Order-statistic characterization and word-size bound #
Structured WordRAM quickselect program (r = 16) #
Output register (also 0 initially in Data.init).
Instances For
Target 0-indexed rank k.
Instances For
Slice length n.
Instances For
Sample a uniform pivot offset tmp ← rand (hi - (lo + 1)) (2 instructions, 1 rand
call).
Instances For
Deterministic 5-instruction setup of selectSlice.
Instances For
Structured 16-register quickselect on Data.init (k :: input): selects input.mergeSort[k]
in [2, input.length + 2) when k < input.length, and sets ans ← 0 otherwise.
Instances For
Compiled flat 16-register randomized quickselect program halting with ans.
Instances For
Quadratic worst-case instruction budget 10 * n ^ 2 + 15.
Instances For
Linear expected instruction budget 80 * n + 15.
Instances For
Selection invariant and subroutine specifications #
Specification of selectSlice on an arbitrary slice [vbase, vbase + n).
When k = x.regs rk < x.regs len = n and Fits w (x.regs base + x.regs len):
selectSliceruns within10 * n ^ 2 + 8instructions andn - 1randcalls, permuting[vbase, vbase + n)in place, placing thek-th sorted element in bothy.mem (vbase + k)andy.regs ans, partitioning[vbase, vbase + n)aroundvbase + k, and modifying onlyFinset.Ico vbase (vbase + n);- Its expected instruction count satisfies
expectedCodeTime w selectSlice x ≤ 80 * n + 8.
Deterministic 4-step execution of loadHeader on Data.init (k :: input).
Structured 7-step execution of code when input.length ≤ k.
Total out-of-range behavior (input.length ≤ k).
Whenever InputFits w (k :: input) holds and input.length ≤ k, program executes
deterministically in 8 instructions returning 0, making 0 rand calls and leaving memory
completely unchanged.
Structured ExecRandWithin and linear expectedCodeTime specification for code when
k < input.length.
In-place randomized quickselect: correctness, zero auxiliary space, worst-case bounds,
rand-call bound, and linear expected running time.
For any input : List ℕ of length n, target rank k < n, and word size w ≥ logWidth 2 n with
InputFits w (k :: input):
programhalts without faulting withinworstCaseTime n = 10 * n ^ 2 + 15instructions on every execution path (RandHaltsWithin);- Every execution path makes at most
n - 1randcalls (RandCallsBounded); - On every supported halting configuration,
programreturnsinput.mergeSort[k]withy.Bounded w, permutesmemSlice 2 (n + 2) y.memin place so thaty.mem (2 + k) = input.mergeSort[k]withy.mem (2 + i) ≤ y.mem (2 + k)fori < kandy.mem (2 + k) ≤ y.mem (2 + i)fork < i < n, and leaves all memory outsideFinset.Ico 2 (n + 2)untouched (Modifies, zero auxiliary heap words); - The expected instruction count satisfies
expectedTime w program (.init (k :: input)) ≤ expectedTimeBound n = 80 * n + 15uniformly over allk < nand unconditionally across duplicate keys.