Insertion sort on small arrays in merge sort
Although merge sort runs in \(\Theta(n \lg n)\) worst-case time and insertion sort runs in \(\Theta(n^2)\) worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the recursion by using insertion sort within merge sort when subproblems become sufficiently small. Consider a modification to merge sort in which \(\frac{n}{k}\) sublists of length \(k\) are sorted using insertion sort and then merged using the standard merging mechanism, where \(k\) is a value to be determined.
a. Show that insertion sort can sort the \(\frac{n}{k}\) sublists, each of length \(k\), in \(\Theta(nk)\) worst-case time.
Sorting a sublist of length \(k\) takes \(\Theta(k^2)\), so sorting \(\frac{n}{k}\) such sublists takes \(\Theta (\frac{n}{k}k^2) = \Theta(nk)\) worst-case time.
b. Show how to merge the sublists in \(\Theta(n \lg(\frac{n}{k}))\) worst-case time.
The MERGE procedure takes \(\Theta(n)\) time, and we must merge 2 sublists at a time which takes \(\Theta(\lg(\frac{n}{k}))\) time. Altogether, this will take \(\Theta(n \lg \frac{n}{k})\) worst-case time.
c. Given that the modified algorith runs in \(\Theta(nk + n \lg \frac{n}{k}))\) worst-case time, what is the largest value of \(k\) as a function of \(n\) for which the modified algorithm has the same running time as standard merge sort, in terms of \(\Theta\)-notation?
Suppose \(k = \lg n\). Then we have \(nk + n \lg \frac{n}{k} = n \lg n + n(\lg n - \lg(\lg{n}))\) which represents the same running time as a standard merge sort: \(\Theta (n \lg n)\). If \(k\) were a larger function of \(n\), the front term of \(nk\) would result in a greater running time.
d. How should we choose \(k\) in practice?
Our chosen value for \(k\) should be the largest value in which running INSERTION-SORT on a list of length \(k\) takes less time than running MERGE-SORT on the same list. See Exercise 1.2-2.