Documentation

CslibFrontier.Algorithms.Lean.Sort.Bitonic.Basic

Batcher sorting on finite tuples #

The algorithm sorts a power-of-two tuple of records by a separate key. Each layer compares corresponding records in two halves and moves whole records. Ascending comparisons retain ties; descending comparisons reverse the ascending result, including ties. The complete sorter is not a stable sorting algorithm.

Main definitions #

Main results #

References #

The number of records at a given depth, with a definitional split into two equal halves.

@[simp]
theorem Cslib.BitonicSort.arity_eq_two_pow (depth : ℕ) :
arity depth = 2 ^ depth

The arity at depth is 2 ^ depth.

def Cslib.BitonicSort.IsBitonic {κ : Type u_2} [LinearOrder κ] {n : ℕ} (input : Fin n → κ) :

Four-point lattice invariant for bitonic merge. The four indices are in cyclic order; the two inequalities exclude alternating high and low values.

def Cslib.BitonicSort.compareLayer {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) {n : ℕ} (ascending : Bool) (input : Fin (n + n) → α) :
Fin (n + n) → α

Compare corresponding records in the two halves. Descending order reverses the ascending comparison, so descending ties are exchanged.

def Cslib.BitonicSort.compareLayer.source {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) {n : ℕ} (ascending : Bool) (input : Fin (n + n) → α) :
Fin (n + n) → Fin (n + n)

The source position selected by a layer; corresponding positions are exchanged together.

def Cslib.BitonicSort.merge {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) :
Bool → (Fin (arity depth) → α) → Fin (arity depth) → α

Merge a bitonic tuple by comparing corresponding halves and recursively merging each half.

def Cslib.BitonicSort.sort {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) :
Bool → (Fin (arity depth) → α) → Fin (arity depth) → α

Sort each half in opposite directions, then merge the resulting bitonic tuple.

theorem Cslib.BitonicSort.compareLayer_perm {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) {n : ℕ} (ascending : Bool) (input : Fin (n + n) → α) :
(List.ofFn (compareLayer key ascending input)).Perm (List.ofFn input)

A layer preserves the multiset of complete records, even when some keys agree.

theorem Cslib.BitonicSort.merge_perm {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) (ascending : Bool) (input : Fin (arity depth) → α) :
(List.ofFn (merge key depth ascending input)).Perm (List.ofFn input)

Bitonic merge preserves the multiset of complete records on every input.

theorem Cslib.BitonicSort.sort_perm {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) (ascending : Bool) (input : Fin (arity depth) → α) :
(List.ofFn (sort key depth ascending input)).Perm (List.ofFn input)

Batcher sorting preserves complete records, not merely their projected keys.

def Cslib.BitonicSort.mergeVector {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) :
Bool → Vector α (arity depth) → Vector α (arity depth)

Bitonic merge on a vector. Each layer and each merged half is stored once, so a record is read from the previous layer without recomputing earlier layers.

def Cslib.BitonicSort.sortVector {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) :
Bool → Vector α (arity depth) → Vector α (arity depth)

Batcher sorting on a vector, storing each sorted half and each merge layer once.

@[simp]
theorem Cslib.BitonicSort.get_mergeVector {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) (ascending : Bool) (input : Vector α (arity depth)) :
(mergeVector key depth ascending input).get = merge key depth ascending input.get

The stored merge agrees with merge.

@[simp]
theorem Cslib.BitonicSort.get_sortVector {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) (ascending : Bool) (input : Vector α (arity depth)) :
(sortVector key depth ascending input).get = sort key depth ascending input.get

The stored sort agrees with sort.

def Cslib.BitonicSort.sortImpl {α : Type u_1} {κ : Type u_2} [LinearOrder κ] (key : α → κ) (depth : ℕ) :
Bool → (Fin (arity depth) → α) → Fin (arity depth) → α

Compiler implementation of sort, reading a stored vector from depth 2 on. Depths 0 and 1 keep the closures, which are cheaper there. The compiler applies it to the index as well, so each executed read rebuilds the vector; callers that read every entry can keep sortVector.

@[csimp]

Proved compiler replacement of sort by sortImpl.