**MergeSort** *Thu Oct 6* # Git ~~~sh # Always start with a git pull git pull ~~~ # Notes A quick note about [What's the reason I can't create generic array types in Java?](https://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java). # Merge [Link to Code](https://github.com/pomonacs622020fa/LectureCode/tree/master/MergeSort) ![](images/2020-10-13-Merge1.jpeg) ![Merge](images/2020-10-13-Merge2.jpeg) Questions: - How many comparisons are made? + Answer: * Worst-case: $n-1$ * Best-case: $\frac{n}{2}$ Consider these examples: 1. Merging [1, 2, 3] with [4, 5, 6] + Comparisons: (1, 4), (2, 4), (3, 4) 1. Merging [1, 3, 5] with [2, 4, 6] + Comparisons: (1, 2), (3, 2), (3, 4), (5, 4), (5, 6) # MergeSort ![](images/2020-10-13-MergeSort2.jpeg) ![MergeSort](images/2020-10-13-MergeSort1.jpeg) Questions: - How many comparisons are made? + Answer: $T(n) = 2T(\frac{n}{2}) + (n-1) = O(n lg(n))$ - How many moves are made? + Answer: $T(n) = 2T(\frac{n}{2}) + (n-1) = O(n lg(n))$ - Is it in-place or not? + Answer: Not in-place - How much extra memory is used? + Answer: $O(n)$ - Is it stable or not? + Answer: Stable # Walk-Through Here is a video and corresponding diagrams from a walk-through of the MergeSort algorithm.
![](images/2020-10-13-WalkThrough1.jpeg) ![Walk-Through](images/2020-10-13-WalkThrough2.jpeg) # Running Time ![MergeSort](images/2020-10-15-MergeSortRunningTimeDiagram.jpg)