2-1 Insertion sort on small arrays in merge sort

Although merge sort runs in Θ(nlgn)\Theta(n\lg n) worst-case time and insertion sort runs in Θ(n2)\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 n/kn / k sublists of length kk are sorted using insertion sort and then merged using the standard merging mechanism, where kk is a value to be determined.

a. Show that insertion sort can sort the n/kn / k sublists, each of length kk, in Θ(nk)\Theta(nk) worst-case time.

b. Show how to merge the sublists in Θ(nlg(n/k))\Theta(n\lg(n / k)) worst-case time.

c. Given that the modified algorithm runs in Θ(nk+nlg(n/k))\Theta(nk + n\lg(n / k)) worst-case time, what is the largest value of kk as a function of nn for which the modified algorithm has the same running time as standard merge sort, in terms of Θ\Theta-notation?

d. How should we choose kk in practice?

a. The worst-case time to sort a list of length kk by insertion sort is Θ(k2)\Theta(k^2). Therefore, sorting n/kn / k sublists, each of length kk takes Θ(k2n/k)=Θ(nk)\Theta(k^2 \cdot n / k) = \Theta(nk) worst-case time.

b. We have n/kn / k sorted sublists each of length kk. To merge these n/kn / k sorted sublists to a single sorted list of length nn, we have to take 22 sublists at a time and continue to merge them. This will result in lg(n/k)\lg(n / k) steps and we compare nn elements in each step. Therefore, the worst-case time to merge the sublists is Θ(nlg(n/k))\Theta(n\lg(n / k)).

c. The modified algorithm has time complexity as standard merge sort when Θ(nk+nlg(n/k))=Θ(nlgn)\Theta(nk + n\lg(n / k)) = \Theta(n\lg n). Assume k=Θ(lgn)k = \Theta(\lg n),

Θ(nk+nlg(n/k))=Θ(nk+nlgnnlgk)=Θ(nlgn+nlgnnlg(lgn))=Θ(2nlgnnlg(lgn))=Θ(nlgn). \begin{aligned} \Theta(nk + n\lg(n / k)) & = \Theta(nk + n\lg n - n\lg k) \\ & = \Theta(n\lg n + n\lg n - n\lg(\lg n)) \\ & = \Theta(2n\lg n - n\lg(\lg n)) \\ & = \Theta(n\lg n). \end{aligned}

d. Choose kk be the largest length of sublist on which insertion sort is faster than merge sort.