**QuickSort** *Thu Oct 15* # QuickSort Running Time [Link to Code](https://github.com/pomonacs622020fa/LectureCode/tree/master/QuickSort) ![QuickSort](images/2020-10-15-QuickSort.jpg) Questions: - What is the recursion tree height? + Answer: * Worst-case: $n$ * Best-case: $lg(n) + 1$ - What is the amount of work done at each level in the tree? + Answer: at each level we call `partition` on all of the remaining elements. So the running time for each level is $O(n)$. - What is the running time of QuickSort? + Answer: * Worst-case: $O(n^2)$ * Average-case: $O(n lg(n))$ * Best-case: $O(n lg(n))$ + Note: this is our first algorithm with an **average** running time. Meaning that if you give it the same input it will exhibit different running times (but the same result). # Running Time Comparison ![Comparing Sorting Algorithms](images/comparison-table.png) # Partition ![Partitioning](images/2020-10-15-Partition.jpg) Questions: - How many comparisons are made? + Answer: $hi-lo$ - How many swaps are made? + Answer: * Worst-case: $hi-lo$ * Best-case: $0$ - Is it in-place or not? + Answer: In-place - How much extra memory is used? + Answer: $0$ - Is it stable or not? + Answer: Not Stable # Walk-Through Here is a video and corresponding diagrams from a walk-through of the QuickSort algorithm.
![Walk-Through](images/2020-10-15-WalkThrough.jpg)