2-1 Insertion sort on small arrays in merge sort
Although merge sort runs in worst-case time and insertion sort runs in 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 sublists of length are sorted using insertion sort and then merged using the standard merging mechanism, where is a value to be determined.
a. Show that insertion sort can sort the sublists, each of length , in worst-case time.
b. Show how to merge the sublists in worst-case time.
c. Given that the modified algorithm runs in worst-case time, what is the largest value of as a function of for which the modified algorithm has the same running time as standard merge sort, in terms of -notation?
d. How should we choose in practice?
a. The worst-case time to sort a list of length by insertion sort is . Therefore, sorting sublists, each of length takes worst-case time.
b. We have sorted sublists each of length . To merge these sorted sublists to a single sorted list of length , we have to take sublists at a time and continue to merge them. This will result in steps and we compare elements in each step. Therefore, the worst-case time to merge the sublists is .
c. The modified algorithm has time complexity as standard merge sort when . Assume ,
d. Choose be the largest length of sublist on which insertion sort is faster than merge sort.