KonkurStudy
همه کارت‌ها

Merge Sort with a Constant-Size Insertion-Sort Cutoff

Stopping merge-sort recursion at blocks of a constant size kk and finishing each block with insertion sort keeps the running time at Θ(nlog⁡n)\Theta(n\log n); the cutoff only shaves a constant factor. The bound changes only when kk is allowed to grow with nn.

کارت مفهوم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

The idea — coarsening merge sort

Plain merge sort recurses all the way down to subarrays of size 11. Coarsening (CLRS Problem 2-1) stops the recursion early: once a subarray shrinks to a fixed small size kk, you no longer split it — you sort that block directly with insertion sort, which is fast and cache-friendly on tiny inputs. The merges then rebuild the array from the sorted blocks as usual.

The whole result hinges on one question: is kk a constant, or does it grow with nn?

The two phases and their cost

Split the work into the insertion-sort phase (sorting the bottom blocks) and the merge phase (combining them upward).

1. Insertion-sort phase. A block of size kk costs Θ(k2)\Theta(k^{2}) in the worst case. There are nk\dfrac{n}{k} blocks, so the total is

nk⋅Θ(k2)=Θ(nk).\frac{n}{k}\cdot\Theta(k^{2})=\Theta(nk).

2. Merge phase. Merging starts at blocks of size kk (not size 11), so the recursion tree is shortened: instead of log⁡2n\log_2 n levels it has only log⁡2 ⁣nk\log_2\!\dfrac{n}{k} levels. Each level still does Θ(n)\Theta(n) merging work, giving

Θ ⁣(nlog⁡nk).\Theta\!\left(n\log\frac{n}{k}\right).

Total:

T(n)=Θ ⁣(nk+nlog⁡nk).T(n)=\Theta\!\left(nk + n\log\frac{n}{k}\right).

Reading the formula — the role of kk

Regime for kk Insertion part nknk Merge part nlog⁡(n/k)n\log(n/k) Total
kk = constant (e.g. k=20k=20) Θ(n)\Theta(n) Θ(nlog⁡n)\Theta(n\log n) Θ(nlog⁡n)\Theta(n\log n)
k=Θ(log⁡n)k=\Theta(\log n) Θ(nlog⁡n)\Theta(n\log n) Θ(nlog⁡n)\Theta(n\log n) Θ(nlog⁡n)\Theta(n\log n)
kk grows faster than log⁡n\log n dominates smaller Θ(nk)\Theta(nk), worse

When kk is a constant, log⁡nk=log⁡n−log⁡k=log⁡n−O(1)=Θ(log⁡n)\log\dfrac{n}{k}=\log n-\log k=\log n-O(1)=\Theta(\log n) and nk=Θ(n)nk=\Theta(n), so

T(n)=Θ(n)+Θ(nlog⁡n)=Θ(nlog⁡n).T(n)=\Theta(n)+\Theta(n\log n)=\Theta(n\log n).

The cutoff is invisible to the asymptotics — it only improves the hidden constant (which is why real libraries do it). The cutoff is provably worthwhile right up to k=Θ(log⁡n)k=\Theta(\log n); beyond that the quadratic insertion-sort term takes over and degrades the bound.

How to apply (recipe)

  1. Identify the cutoff size kk and ask whether it is constant or a function of nn.
  2. Insertion phase →Θ(nk)\to\Theta(nk); merge phase →Θ ⁣(nlog⁡nk)\to\Theta\!\left(n\log\frac{n}{k}\right).
  3. Add them and simplify, treating any constant kk as O(1)O(1).

Mini-example

Merge sort that switches to insertion sort on every subarray of size ≤20\le 20. Here k=20k=20 is constant: insertion phase =Θ(20n)=Θ(n)=\Theta(20n)=\Theta(n), merge phase =Θ(nlog⁡n20)=Θ(nlog⁡n)=\Theta(n\log\frac{n}{20})=\Theta(n\log n), so the algorithm is still

T(n)=Θ(nlog⁡n).T(n)=\Theta(n\log n).

Exam trick: A fixed cutoff ("sort blocks of 2020 / of 100100 with insertion sort") is a constant — it cannot change the Θ\Theta. The answer stays Θ(nlog⁡n)\Theta(n\log n). Only a cutoff that scales with nn matters: k=Θ(log⁡n)k=\Theta(\log n) is the break-even point (still Θ(nlog⁡n)\Theta(n\log n)), and anything larger makes the insertion-sort term Θ(nk)\Theta(nk) dominate.

فرمول‌های کلیدی

Total cost of coarsened merge sort
T(n)=Θ ⁣(nk+nlog⁡nk)T(n)=\Theta\!\left(nk + n\log\frac{n}{k}\right)

Insertion-sort phase on n/k blocks of size k, plus merging from blocks of size k upward.

Insertion-sort phase
nk⋅Θ(k2)=Θ(nk)\frac{n}{k}\cdot\Theta(k^{2})=\Theta(nk)

n/k blocks, each costing Theta(k^2) in the worst case; collapses to Theta(n) when k is constant.

Merge phase with raised leaves
Θ ⁣(nlog⁡nk)\Theta\!\left(n\log\frac{n}{k}\right)

Merging starts at blocks of size k, so the recursion tree has log_2(n/k) levels of Theta(n) work each.

Constant-cutoff result
k=O(1)  ⟹  T(n)=Θ(nlog⁡n)k=O(1)\;\Longrightarrow\; T(n)=\Theta(n\log n)

A fixed block size only changes the constant factor; the asymptotic bound is unchanged.

#merge-sort#insertion-sort#cutoff#coarsening#hybrid-sort#sorting#n-log-n#asymptotics#constant-factor#clrs-2-1

سوالات تمرینی