Insertion sort
AbbreviationCharged comparison#✓fully proved
For elements \(x\) and \(y\) of a linearly ordered type, this computation returns the truth value of \(x \le y\) and charges one unit of time.
Used by 11
DefinitionCosted insertion into a list#✓fully proved
Let \(a\) be an element of a linearly ordered type. Inserting \(a\) into the empty list returns \([a]\) at no cost. Inserting \(a\) into a list with head \(b\) and tail \(l\) first charges one unit for comparing \(a \le b\); if \(a \le b\) it returns \(a\) followed by \(b\) and \(l\), and otherwise it inserts \(a\) into \(l\), adding that cost, and returns \(b\) followed by the result.
def insertionSortInsert (a : α) : List α → TimeM Nat (List α)
| [] => pure [a]
| b :: xs => TimeM.bind (compareLE a b) fun before =>
if before then pure (a :: b :: xs)
else TimeM.bind (insertionSortInsert a xs) fun tail => pure (b :: tail)
abbrev compareLE (x y : α) : TimeM Nat Bool := do
✓ return decide (x ≤ y)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:37
Uses 1 · Used by 6
DefinitionInsertion sort with comparison cost#✓fully proved
On a list over a linearly ordered type, insertion sort returns the empty list at no cost when the input is empty. On a list with head \(a\) and tail \(l\), it sorts \(l\) and then inserts \(a\) into the result by costed insertion; its cost is the cost of sorting \(l\) plus the cost of that insertion. Exactly one unit is charged for each order comparison.
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
def insertionSortInsert (a : α) : List α → TimeM Nat (List α)
| [] => pure [a]
| b :: xs => TimeM.bind (compareLE a b) fun before =>
if before then pure (a :: b :: xs)
else TimeM.bind (insertionSortInsert a xs) fun tail => pure (b :: tail)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:41
Uses 1 · Used by 22
TheoremCosted insertion returns the ordered insertion#✓fully proved
For an element \(a\) and a list \(l\) over a linearly ordered type, the list returned by costed insertion of \(a\) into \(l\) is the ordered insertion of \(a\) into \(l\) with respect to \(\le\): \(a\) is placed immediately before the first element \(b\) of \(l\) with \(a \le b\), or at the end of \(l\) if there is no such element.
theorem ret_insertionSortInsert (a : α) (xs : List α) :
(insertionSortInsert a xs).ret = List.orderedInsert (fun x y => x ≤ y) a xs
def insertionSortInsert (a : α) : List α → TimeM Nat (List α)
| [] => pure [a]
| b :: xs => TimeM.bind (compareLE a b) fun before =>
if before then pure (a :: b :: xs)
else TimeM.bind (insertionSortInsert a xs) fun tail => pure (b :: tail)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:41
Uses 2 · Used by 1
TheoremCost-annotated insertion sort returns the standard insertion sort#✓fully proved
For every list \(xs\) over a linearly ordered type, the list returned by insertion sort with comparison costs equals the standard insertion sort of \(xs\) with respect to \(\le\), which inserts the head into the sorted tail.
@[simp]
theorem ret_insertionSort (xs : List α) :
(insertionSort xs).ret = List.insertionSort (fun x y => x ≤ y) xs
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:48
Uses 3 · Used by 3
TheoremAgreement with CSLib's monadic insertion sort#✓fully proved
For a list \(l\) over a linearly ordered type in the lowest universe, the costed insertion sort of \(l\) equals CSLib's generic monadic insertion sort run in the time-counting monad with the charged comparison: the returned lists agree and the numbers of charged comparisons agree.
@[simp]
theorem insertionSort_eq_insertionSortM {β : Type} [LinearOrder β] (xs : List β) :
insertionSort xs = List.insertionSortM compareLE xs
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:48
abbrev compareLE (x y : α) : TimeM Nat Bool := do
✓ return decide (x ≤ y)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:37
Uses 3 · Used by 7
TheoremInsertion sort returns a sorted list#✓fully proved
For every list \(l\) over a linearly ordered type, the list returned by insertion sort on \(l\) is nondecreasing.
theorem insertionSort_sorted (xs : List α) :
List.Pairwise (fun x y => x ≤ y) (insertionSort xs).ret
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:48
Uses 2 · Used by 2
TheoremInsertion sort permutes its input#✓fully proved
For every list \(l\) over a linearly ordered type, the list returned by insertion sort is a permutation of \(l\); in particular every element occurs in it exactly as often as in \(l\).
theorem insertionSort_perm (xs : List α) : List.Perm (insertionSort xs).ret xs
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:48
Uses 2 · Used by 12
TheoremCorrectness of insertion sort#✓fully proved
For every list \(l\) over a linearly ordered type, the list returned by insertion sort is nondecreasing and is a permutation of \(l\).
theorem insertionSort_correct (xs : List α) :
List.Pairwise (fun x y => x ≤ y) (insertionSort xs).ret ∧
List.Perm (insertionSort xs).ret xs
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:48
Uses 3
TheoremCost of a single insertion#✓fully proved
For an element \(a\) and a list \(l\) over a linearly ordered type, costed insertion of \(a\) into \(l\) charges at most \(n\) comparisons, where \(n\) is the length of \(l\).
theorem insertionSortInsert_time_le (a : α) (xs : List α) :
(insertionSortInsert a xs).time ≤ xs.length
def insertionSortInsert (a : α) : List α → TimeM Nat (List α)
| [] => pure [a]
| b :: xs => TimeM.bind (compareLE a b) fun before =>
if before then pure (a :: b :: xs)
else TimeM.bind (insertionSortInsert a xs) fun tail => pure (b :: tail)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:41
Uses 2 · Used by 1
TheoremComparison bound for insertion sort#✓fully proved
On a list of length \(n\) over a linearly ordered type, insertion sort charges at most \(n(n-1)/2\) comparisons.
theorem insertionSort_time (xs : List α) :
(insertionSort xs).time ≤ xs.length * (xs.length - 1) / 2
def insertionSort : List α → TimeM Nat (List α)
| [] => pure []
| a :: xs => TimeM.bind (insertionSort xs) (insertionSortInsert a)
CSLibExt/Algorithms/Lean/Sort/InsertionCost.lean:48
Uses 4 · Used by 6
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.