LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 9 Part 1 of 3
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

What are the three steps of divide-and-conquer, and where does the recursion stop?

Answer

S n S1S2 divide: two disjoint parts each part: the same algorithm again 0 or 1 element: already sorted, no work the base case: the floor of the recursion n n/2 n/4 1 strictly smaller at every level sorted combine, level by level, back to the top merge-sort: blind cut · works when combining quick-sort: works when splitting · plain join divide · recur · conquerstop at 0 or 1 element

Explanation in 6 steps

Divide the input into disjoint parts, recur by solving each part with the same algorithm, then conquer by combining the partial solutions into the solution for the whole. The recursion stops at subproblems of size 0 or 1, which need no work at all.

Divide-and-conquer is a design pattern, not one particular algorithm. For sorting a sequence $S$ it reads:

  1. Divide: split $S$ into (usually two) disjoint parts $S_1$ and $S_2$.
  2. Recur: sort $S_1$ and $S_2$ by calling the same algorithm on each part.
  3. Conquer: combine the two sorted parts into the sorted $S$.

The recursion needs a floor, the base case: a sequence with 0 or 1 elements is already sorted, so the call returns it unchanged. Every other call hands strictly smaller problems to its recursive calls, so the recursion is guaranteed to reach that floor. Insertion-sort starts from the same fact: its sorted part begins as the first element alone, because a single element is always sorted.

The pattern leaves open where the real work happens, and the two classic sorts answer that in opposite ways: merge-sort splits blindly in the middle and does its comparisons when combining, quick-sort does its comparisons when splitting and combines by simply writing the parts one after the other.

Go deeper:

or press any other key

Question

How does merge-sort sort a sequence of $n$ elements?

Answer

72943861 n/2 7 2 9 4 3 8 6 1 no value looked at the divide: a cut by position 72943861 24791368 merge-sort the recursion sorts each half 1 2 3 4 6 7 8 9 all comparisons happen here the conquer: merge the sorted halves size 0 or 1: returned as it is the base case if S.size() > 1(S1, S2) ← partition(S, n/2)S1 ← mergeSort(S1, C)S2 ← mergeSort(S2, C)S ← merge(S1, S2)return S partition here: a cut, no comparisons if S.size() > 1(S1, S2) ← partition(S, n/2)S1 ← mergeSort(S1, C)S2 ← mergeSort(S2, C)S ← merge(S1, S2)return S the cut ignores the values

Explanation in 7 steps

Cut the sequence into two halves of about $n/2$ elements, sort each half recursively with merge-sort, then merge the two sorted halves into one sorted sequence. Sequences of 0 or 1 elements are returned as they are.

Algorithm mergeSort(S, C)          // C: comparator that defines the order
  if S.size() > 1
    (S1, S2) ← partition(S, n/2)   // first n/2 elements, and the rest
    S1 ← mergeSort(S1, C)
    S2 ← mergeSort(S2, C)
    S  ← merge(S1, S2)
  return S

The three divide-and-conquer steps map one to one: partition is the divide, the two recursive calls are the recur, and merge is the conquer. The partition here only cuts the sequence at position $n/2$, without looking at a single value; it has nothing to do with quick-sort's partition step, despite the name. All comparisons happen inside merge. That is why merge-sort hardly cares about the input order: the cuts are always in the middle, whatever the values are.

The comparator $C$ makes the algorithm generic: the same code sorts numbers, strings or records, ascending or descending, depending on which comparison is passed in.

Go deeper:

  • doc Merge sort — top-down and bottom-up versions, analysis, and variants for linked lists and external storage.
  • tool VisuAlgo: sorting — run merge-sort and quick-sort step by step on your own input.
Merge-sort on seven values: the list is split down to one-element lists, which are trivially sorted, then merged back up in order
Merge-sort on seven values: the list is split down to one-element lists, which are trivially sorted, then merged back up in order
VineetKumar at English Wikipedia · Public domain · Wikimedia Commons
or press any other key