Documentation

CslibFrontier.Foundations.Data.List.MergeSort

Agreement of stable merge sort and insertion sort #

For a total transitive relation, merge sort and insertion sort return the same list, including the order of elements tied by the relation. This connects a divide-and-conquer implementation to the right fold used by insertion sort, for both symbolic proofs and machine certificates.

theorem List.mergeSort_decide_eq_insertionSort {α : Type u_1} (r : α → α → Prop) [DecidableRel r] (htrans : ∀ (a b c : α), r a b → r b c → r a c) (htotal : ∀ (a b : α), r a b ∨ r b a) (xs : List α) :
(xs.mergeSort fun (a b : α) => decide (r a b)) = insertionSort r xs

The two stable sorts agree for every total transitive relation, even when distinct entries are tied.