Documentation

Complexity.Computability.Circuit.Boolean.KeyedSort

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 #

Main results #

References #

def Cslib.Circuits.Boolean.keyedSortCircuit {k r : ℕ} (key : Circuit signature r k) (depth : ℕ) (ascending : Bool) :

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.

      theorem Cslib.Circuits.Boolean.cost_program_keyedSortCircuit {k r : ℕ} (key : Circuit signature r k) (depth : ℕ) (ascending : Bool) (cost : signature.Op → ℕ) :
      (keyedSortCircuit key depth ascending).program.cost cost = BitonicSort.arity depth * key.program.cost cost + (bitonicSortCircuit (Fin.castAdd r) depth ascending).program.cost cost

      Key computation is charged once per record; packing and dropping keys are free wirings.

      theorem Cslib.Circuits.Boolean.four_mul_cost_program_keyedSortCircuit {k r : ℕ} (key : Circuit signature r k) (depth : ℕ) (ascending : Bool) (cost : signature.Op → ℕ) :
      4 * (keyedSortCircuit key depth ascending).program.cost cost = 4 * 2 ^ depth * key.program.cost cost + depth * (depth + 1) * 2 ^ depth * (compareSwapCircuit (Fin.castAdd r)).program.cost cost

      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.

      theorem Cslib.Circuits.Boolean.four_mul_size_keyedSortCircuit {k r : ℕ} (key : Circuit signature r k) (depth : ℕ) (ascending : Bool) :
      4 * (keyedSortCircuit key depth ascending).size = 4 * 2 ^ depth * key.size + depth * (depth + 1) * 2 ^ depth * if k + r = 0 then 0 else 6 * k + 6 * (k + r) + 2

      Exact total gate count, including constants and negations.