0
$\begingroup$

Quick question on the wording of the question from CLRS Ch2 Problems. The question goes as follows:

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.

My question is: shouldn't there be $k$ sublists each of length $n/k$? The other way around is stated in the question. Because in merge sort, we keep halving the array, thus producing twice the number of arrays as in the previous level. Hence, each sublist is of length $n/k$ and we have $k$ of such sublists. Why does the question mention this the other way around?

$\endgroup$
1
  • $\begingroup$ Because there is no difference. Finding $k$ such that $n/k$ sublists of length $k$ ... is the same as finding $k' = n/k$ such that $k'$ sublists of length $n/k'$. $\endgroup$
    – user114966
    Feb 15, 2021 at 4:22

2 Answers 2

1
$\begingroup$

The idea most probably is to chose sublist size $k$ such that merge sort likely is faster for bigger problem instances and insertion sort for smaller ones.
$\lceil s = n/k\rceil$ sub-lists may be suboptimal for merging lists of disparate size later on.
One alternative is splitting into $2^{\lfloor\log_2{n/k}\rceil}$ lists.

$\endgroup$
0
$\begingroup$

The proposed algorithm has the following structure. On an input of length $n$:

  • If $n$ is large, then divide the input into two parts of sizes $\lfloor n/2 \rfloor$ and $\lceil n/2 \rceil$, sort of each of them recursively, and then merge them.
  • If $n$ is small, then instead use insertion sort.

When should we switch to insertion sort? When the list size is smaller than some constant $k$.

If you think of the entire recursion tree, then we switch to insertion sort when having $n/k$ sublists of length $k$, and not the other way around. Switching when we have $k$ sublists of length $n/k$ would mean switching at a constant recursion depth, which makes little sense here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.