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 #
Cslib.BitonicSort.arity: the power-of-two number of records, with a definitional split.Cslib.BitonicSort.IsBitonic: the four-point lattice invariant used by bitonic merge.Cslib.BitonicSort.compareLayer: one layer of independent comparisons.Cslib.BitonicSort.merge: recursively merge a bitonic tuple.Cslib.BitonicSort.sort: Batcher's recursive sorter.
Main results #
Cslib.BitonicSort.compareLayer_perm,Cslib.BitonicSort.merge_perm, andCslib.BitonicSort.sort_perm: each stage preserves the multiset of complete records.
References #
- K. E. Batcher, Sorting Networks and Their Applications.
- Adapted from MassProduction/SortingSemantics/Defs.lean and MassProduction/SortingSemantics/Internal.lean in S. Schlesinger, Boolean circuit mass production formalization.
The number of records at a given depth, with a definitional split into two equal halves.
Instances For
Four-point lattice invariant for bitonic merge. The four indices are in cyclic order; the two inequalities exclude alternating high and low values.
Instances For
Compare corresponding records in the two halves. Descending order reverses the ascending comparison, so descending ties are exchanged.
Instances For
A layer preserves the multiset of complete records, even when some keys agree.
Bitonic merge preserves the multiset of complete records on every input.
Batcher sorting preserves complete records, not merely their projected keys.