DAA Study Hub

7 · Divide & conquer + merge sort

Split, solve, combine: merge sort traced and analysed.

In one sentence

Divide & conquer splits a problem into smaller copies of itself, solves them recursively, and combines the answers. Merge sort splits the array in half, sorts each half, then merges two sorted halves in linear time, for Θ(nlog⁡n)\Theta(n\log n) in every case.

Everyday analogy

Sorting exam papers with friends. You have 800 papers. Give half to each of two friends; they each split their pile again, and so on, until each person holds one paper (already "sorted"). Then everyone walks back up the chain: two people with sorted piles merge them by repeatedly taking the smaller top sheet. Merging two sorted piles is fast, because you only ever look at the two top sheets.

Key ideas

StepMerge sortCost
Dividefind the middle qqΘ(1)\Theta(1)
Conquersort A[p..q] and A[q+1..r] recursively2T(n/2)2T(n/2)
CombineMerge the two sorted halvesΘ(n)\Theta(n)
T(n)={Θ(1)n=12T(n/2)+Θ(n)n>1⇒T(n)=Θ(nlog⁡n) (solved in Topic 8)T(n) = \begin{cases}\Theta(1) & n = 1\\ 2T(n/2) + \Theta(n) & n > 1\end{cases} \quad\Rightarrow\quad T(n) = \Theta(n\log n) \text{ (solved in Topic 8)}
MergeSort(A, p, r)
1  if p ≥ r then return                // 0 or 1 element: already sorted
2  q ← ⌊(p + r) / 2⌋
3  MergeSort(A, p, q)
4  MergeSort(A, q + 1, r)
5  Merge(A, p, q, r)

Merge(A, p, q, r)                      // A[p..q] and A[q+1..r] are each sorted
1  copy A[p..q] into L, A[q+1..r] into R
2  i ← 1; j ← 1; k ← p
3  while i ≤ |L| and j ≤ |R| do        // both piles still have cards
4      if L[i] ≤ R[j] then A[k] ← L[i]; i ← i + 1
5      else               A[k] ← R[j]; j ← j + 1
6      k ← k + 1
7  copy whatever is left in L or R into A[k..r]

Tree facts used in the analysis (complete kk-ary tree): level dd has kdk^d nodes, and a tree with nn leaves has height log⁡kn\log_k n. For merge sort (k=2k = 2): log⁡2n\log_2 n splitting levels.


Merge sort on [6, 2, 9, 4, 1, 7, 3, 8] (our own example)

Merging back up (comparisons in brackets):

LevelMergesResult
size 2[6]+[2]→2 6 (1) · [9]+[4]→4 9 (1) · [1]+[7]→1 7 (1) · [3]+[8]→3 8 (1)4 comparisons
size 4[2 6]+[4 9]→2 4 6 9 (3) · [1 7]+[3 8]→1 3 7 8 (3)6 comparisons
size 8[2 4 6 9]+[1 3 7 8]→1 2 3 4 6 7 8 9 (7)7 comparisons

Total 17 comparisons. Each level does at most n=8n = 8 work, and there are log⁡28=3\log_2 8 = 3 merge levels.

Inside one merge: L = [2 4 6 9], R = [1 3 7 8]

comparetakeoutput so far
2 vs 11 (R)1
2 vs 32 (L)1 2
4 vs 33 (R)1 2 3
4 vs 74 (L)1 2 3 4
6 vs 76 (L)1 2 3 4 6
9 vs 77 (R)1 2 3 4 6 7
9 vs 88 (R)1 2 3 4 6 7 8
R emptycopy 91 2 3 4 6 7 8 9

Common mistakes

  • In a trace, merging unsorted halves. Always recurse all the way down first.
  • Forgetting to copy the leftovers at the end of Merge.
  • Saying merge sort is Θ(n)\Theta(n) in the best case. It does the same splitting and merging on every input.
  • Miscounting levels: there are log⁡2n\log_2 n merge levels plus the leaf level, so log⁡2n+1\log_2 n + 1 levels in the tree.

Mastery check

Solve without hints, then explain

Trace merge sort on [5, 1, 4, 2, 8, 3] (left half gets the extra element when splitting an odd size). Show every merge and count the total comparisons.

Your mastery check
Not started
Solve the problem above with no hints, close the page and explain it out loud. Tick each step honestly.
Mastery checklist

Status

Tick all six steps to unlock “Mastered”.

Which study mode worked for you?

Saved in this browser onlySee all my progress →

See it move

References

  • 08 Divide & Conquer, Recurrences: Slides 3–4 (k-ary tree facts), 5 (D&C idea), 7–19 (merging step by step), 20–24 (MERGE and MERGE-SORT procedures), 25–29 (running time and recursion tree).
  • CLRS 4e §2.3 (designing algorithms: merge sort), Appendix B.5 (trees).

On this page