Basic properties of mergeSort
. #
mergeSort_sorted
:mergeSort
produces a sorted list.mergeSort_perm
:mergeSort
is a permutation of the input list.mergeSort_of_sorted
:mergeSort
does not change a sorted list.mergeSort_cons
: provesmergeSort le (x :: xs) = l₁ ++ x :: l₂
for somel₁, l₂
so thatmergeSort le xs = l₁ ++ l₂
, and noa ∈ l₁
satisfiesle a x
.mergeSort_stable
: ifc
is a sorted sublist ofl
, thenc
is still a sublist ofmergeSort le l
.
splitInTwo #
enumLE #
merge #
If the ordering relation le
is transitive and total (i.e. le a b ∨ le b a
for all a, b
)
then the merge
of two sorted lists is sorted.
mergeSort #
The result of mergeSort
is sorted,
as long as the comparison function is transitive (le a b → le b c → le a c
)
and total in the sense that le a b ∨ le b a
.
The comparison function need not be irreflexive, i.e. le a b
and le b a
is allowed even when a ≠ b
.
If the input list is already sorted, then mergeSort
does not change the list.
This merge sort algorithm is stable, in the sense that breaking ties in the ordering function using the position in the list has no effect on the output.
That is, elements which are equal with respect to the ordering function will remain in the same order in the output list as they were in the input list.
See also:
mergeSort_stable
: ifc <+ l
andc.Pairwise le
, thenc <+ mergeSort le l
.mergeSort_stable_pair
: if[a, b] <+ l
andle a b
, then[a, b] <+ mergeSort le l
)
Another statement of stability of merge sort.
If c
is a sorted sublist of l
,
then c
is still a sublist of mergeSort le l
.
Another statement of stability of merge sort.
If a pair [a, b]
is a sublist of l
and le a b
,
then [a, b]
is still a sublist of mergeSort le l
.