Sorting records by a computed key #
Compute each record's key once, sort the enriched records, and discard the temporary keys. The same record layout can therefore support successive sorts with different keys. Keys are ordered lexicographically, with coordinate zero most significant. Complete records, including duplicates and payload, are preserved; sorting need not be stable.
Main definitions #
Cslib.Circuits.Boolean.keyedSortCircuit: Batcher sorting by a supplied key circuit.
Main results #
Cslib.Circuits.Boolean.keyedSortCircuit_perm: preservation of complete records.Cslib.Circuits.Boolean.monotone_keyedSortCircuitandCslib.Circuits.Boolean.antitone_keyedSortCircuit: recomputed output keys are sorted.Cslib.Circuits.Boolean.four_mul_cost_program_keyedSortCircuit: exact weighted cost, charging key computation once per record. This does not certify constructor runtime.
References #
- Adapted from MassProduction/Nonuniform/KeyedSort.lean in S. Schlesinger, Boolean circuit mass production formalization.
- K. E. Batcher, Sorting Networks and Their Applications.
Compute keys, sort the enriched records, and retain the original record bodies.
Instances For
Prepend the computed key to each complete record.
Instances For
Packing retains each record after its computed key.
Each output record is the body of the corresponding sorted enriched record.
Sorting by computed keys preserves the multiset of complete records.
Recomputing the keys of ascending output records gives a nondecreasing sequence.
Recomputing the keys of descending output records gives a nonincreasing sequence.
Key computation is charged once per record; packing and dropping keys are free wirings.
Exact weighted cost, including all temporary key gates and comparisons. The factor four avoids natural division and includes depth zero, empty keys, and empty records.