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⌉.
TimeM.ret passes through List.mergeSortM into the comparator.
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.
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.
Instances For
x is a minimum element of list l if x ≤ b for all b ∈ l.
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)
Instances For
Upper bound function for merge sort time complexity: T(n) = n * ⌈log₂ n⌉
Instances For
Solve the recurrence