MergeSort on a list #
In this file we introduce merge and mergeSort algorithms that returns a time monad
over the list TimeM (List α). The time complexity of mergeSort is the number of comparisons.
--
Main results #
mergeSort_correct:mergeSortpermutes the list into a sorted one.mergeSort_time: The number of comparisons ofmergeSortis at mostn*⌈log₂ n⌉.
Merges two lists into a single list, counting comparisons as time cost.
Returns a TimeM (List α) where the time represents the number of comparisons performed.
Equations
- One or more equations did not get rendered due to their size.
- Cslib.Algorithms.Lean.TimeM.merge [] x✝ = pure x✝
- Cslib.Algorithms.Lean.TimeM.merge x✝ [] = pure x✝
Instances For
Sorts a list using the merge sort algorithm, counting comparisons as time cost.
Returns a TimeM (List α) where the time represents the total number of comparisons.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A list is sorted if it satisfies the Pairwise (· ≤ ·) predicate.
Equations
- Cslib.Algorithms.Lean.TimeM.IsSorted l = List.Pairwise (fun (x1 x2 : α) => x1 ≤ x2) l
Instances For
x is a minimum element of list l if x ≤ b for all b ∈ l.
Equations
- Cslib.Algorithms.Lean.TimeM.MinOfList x l = ∀ b ∈ l, x ≤ b
Instances For
Recurrence relation for the time complexity of merge sort.
For a list of length n, this counts the total number of comparisons:
- Base cases: 0 comparisons for lists of length 0 or 1
- Recursive case: split the list, sort both halves,
then merge (which takes at most
ncomparisons)
Equations
- One or more equations did not get rendered due to their size.
- Cslib.Algorithms.Lean.TimeM.timeMergeSortRec 0 = 0
- Cslib.Algorithms.Lean.TimeM.timeMergeSortRec 1 = 0
Instances For
Upper bound function for merge sort time complexity: T(n) = n * ⌈log₂ n⌉
Equations
Instances For
Solve the recurrence