# Mergesort

Chapters
1. [00:00] Mergesort
2. [02:36] Fragen
3. [02:44] Wie funktioniert der Algorithmus?
4. [03:04] Was ist die asymptotische Komplexität des Algorithmus?
5. [03:23] Was macht Merge?
6. [03:34] Wie lautet die Invariante?
7. [03:58] Warum ist der Algorithmus korrekt?
8. [04:10] Wie wird die Invariante sichergestellt?
9. [04:31] Was ist die asymptotische Komplexität des Algorithmus?

## General Information

Algorithmic problem: Sorting based on pairwise comparison

Type of algorithm: recursion

## Abstract View

Invariant: After a recursive call, the input sequence of this recursive call is sorted.

Variant: For a recursive call on a subsequence $S'$ of $S$, let $S_1'$ and $S_2'$ denote the subsequences of $S'$ with which Mergesort is called recursively from that call. Then it is $|S_1'| \leq \lceil|S'| /2\rceil$ and $|S_2'| \leq \lceil|S'| /2\rceil$.

Break condition: The current subsequence of the recursive call is a singleton.

Remark: For a particular recursive call $C$, we may, for example, choose the height of the recursion subtree with root $C$ as the induction parameter. For conciseness, the induction parameter is omitted in the following.

## Induction Basis

Abstract view: Nothing to do on a singleton.

Implementation: Ditto.

Proof: A singleton is trivially sorted.

## Induction Step

Abstract view: The sequence is divided into two subsequences of approximately half size, it does not matter at all in which way this is done. Both subsequences are sorted recursively using Mergesort. The sorted subsequences are "merged" into one using algorithm Merge.

Implementation: Obvious.

Correctness: By induction hypothesis, the recursive calls sort correctly. So, correctness of Merge implies correctness of Mergesort.

## Complexity

Statement: The complexity is in $O(T\cdot n \log n)$ in the best and worst case, where $T$ is the complexity of the comparison.

Proof: Obviously, the variant is correct. So, the lengths of $S_1'$ and $S_2'$ are at most $\lceil1/2\rceil$ of the length of $S'$. Consequently, the lengths of $S_1'$ and $S_2'$ are at least $\lfloor1/2\rfloor$ of the length of $S'$. In summary, the overall recursion depth is in $\Theta(\log n)$ in the best and worst case. Next consider the run time of a single recursive call, which receives some $S'$ as input and calls Mergesort recursively with two subsequences $S_1'$ and $S_2'$. The run time of this recursive call (excluding the run times of the recursive calls with $S_1'$ and $S_2'$) is linear in the length of $S'$. Since all recursive calls on the same recursion level operate on pairwise disjoint subsequences, the total run time of all calls on the same recursive level is linear in the length of the original sequence.

## Example implementations

### Java

	public static <T> void mergesort(List<T> liste, Comparator<T> cmp) {
if (liste.size() <= 1)
return;
zerlegeInTeillisten(liste, teilliste1, teilliste2);
mergesort(teilliste1, cmp);
mergesort(teilliste2, cmp);
liste.clear();
merge(teilliste1, teilliste2, liste, cmp);
}

// -----------------
public static <T> void zerlegeInTeillisten(List<T> liste,
List<T> teilliste1, List<T> teilliste2) {
ListIterator<T> it = liste.listIterator();
for (int i = 0; i < liste.size(); i++) {
T elem = it.next();
if (i <= liste.size() / 2)
teilliste1.add(elem); // Haengt elem hinten an teilliste1 an
else
}