CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Searching
- Sorting
- Sorting Links
- SelectionSort vs MergeSort
- Definitions
- Selection Sort: repeatedly select largest remaining element and swap it into sorted position
- Mergesort: sort blocks of 1's into 2's, 2's into 4's, etc, on each pass merging sorted blocks into sorted larger blocks
- Analysis
This is mostly informal, and all you need to know for a 112-level analysis of these algorithms. You can easily find much more detailed and rigorous proofs on the web.
- selectionsort
On the first pass, we need N compares and swaps (N-1 compares and 1 swap).
On the second pass, we need only N-1 (since one value is already sorted).
On the third pass, only N-2.
So, total steps are about 1 + 2 + ... + (N-1) + N = N(N+1)/2 = O(N2).
- mergesort
On each pass, we need about 3N compares and copies (N compares, N copies down, N copies back).
So total cost = (3N steps per pass) x (# of passes)
After pass 0, we have sorted lists of size 20 (1)
After pass 1, we have sorted lists of size 21 (2)
After pass 2, we have sorted lists of size 22 (4)
After pass k, we have sorted lists of size 2k
So we need k passes, where N = 2k
So # of passes = k = log2N
Recall that total cost = (3N steps per pass) x (# of passes)
So total cost = (3N)(log2N) = O(NlogN).
Note: This is the theoretical best-possible Big-O for comparison-based sorting!