Problem 2 (200 pts).
(a) (40 pts) Recall the insertion sort algorithm from the HW 2 programming assignment. Insertion sort can
be expressed recursively as follows: to sort A[1..n], we sort A[1..n - 1] and then insert A[n] into the sorted
A[1..n - 1]. Write a runtime recurrence for this and solve it.
(b) (40 pts) Recall that we have defined an inversion as follows: for an array A of length n, indices i and j are
inverted if i < j but A[j] < A[i]. Which array of length n maximizes the number of inversions, and which
one minimizes it?
(c) (40 pts) Reframe the insertion sort algorithm in terms of inversions and justify your experimental observations in the HW 2 programming assignment with this fact as well as the runtime you computed in
Part (a).
(d) Although the worst-case performance of merge sort is better than the worst-case performance of insertion
sort, it is known that insertion sort can perform better on small inputs. In particular, insertion sort runs
in $O(n^2)$ in the worst case and merge sort in $O(n \log n)$.
(a) (40 pts) Suppose we stop recursion in merge sort when the lists are of size k. Note that there will be
n/k such lists. Argue that the runtime needed to sort these lists using insertion sort is $\Theta(nk)$.
(b) (40 pts) Using the standard merge operation, we can merge the sublists in $\Theta(n \log(n/k))$ time, for
a total worst case runtime of $O(nk + n \log(n/k))$. Determine the largest value of k for which this
modified merge sort is no worse than the standard merge sort Hint: make k depend on n.