Skip to content

2-1 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 $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 $n/k$ sublists, each of length k, in $\Theta (nk)$ worst-case time.

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

c. Given that the modified alogotithm runs in $\Theta (nk+n \lg (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?

d. How should you choose k in practice?

a

Each sublists of length can be sorted in $\Theta (k^2)$, there are n/k sublists, so in n $n/k \cdot \Theta(k^2) = \Theta (nk)$ sorst-case time.

b

Each level of merging need to merge $n$ elements, and there are $\lg (n/k)$levels(since we merge each two sublists in one in each level). So the worse-case time to merge the sublists is $\Theta (n \lg (n/k))$.

c

$$ \begin{aligned} & if \quad \Theta (nk+n \lg (n/k)) = \Theta (n \lg n)\cr & \exist \quad c_{1} , c_{2} , n_{0} \quad satisfies:\cr & c_{1}(n \lg n) \leq nk+n \lg (n/k) \leq c_{2}(n \lg n) \quad for \quad n > n_{0}\cr &(c_{1}-1)(\lg n )\leq k - \lg k \leq (c_{2}-1)(\lg n ) \quad for \quad n > n_{0}\cr & since \quad lg k \llless k, \quad \text{we have } k = \Theta (\lg n)\cr \end{aligned} $$

d

Choose k be the largest size of the sublists that insertion-sort is faster than merge-sort.