- Insertion sort runtime can be bounded by # of inversions (# of out-of-order pairs)
- Each swap fixes 1 inversion → total runtime = # of swaps = # of inversions
- Runtime proportional to how much work needed to be done
-
Almost sorted = small # of inversions → linear # of inversions out of possible
$$N^{2}$$ - Insertion sort runtime always
$$\Theta(N + K)$$ ,$$K$$ = # of inversions
- Heapsort = flavor of selection sort → still sorts by finding smallest/largest item each time
-
$$D$$ = # of entries in dictionary - Binary search on tape
$$\Theta(N)$$ b/c physical movement about halved each time →$$\frac{N}{2} + \frac{N}{4} + \cdots + 1 = N - 1$$
- Given array, partition on pivot → after partitioning, pivot in sorted position (invariant)
- Partitioning = single item sort
- Quicksort runtime non-deterministic, depends on where pivot lands
- Best case, pivot always lands in middle
- Counting inversions takes
$$\Theta(N \log{N})$$ time