3.6.2 · D1Sorting & Searching

Foundations — Merge sort — divide and conquer, O(n log n), stable, proof of correctness

1,961 words9 min readBack to topic

This page assumes you have seen nothing. Before you read the parent note, you should recognise every squiggle it throws at you. We build them one at a time, each on top of the one before it, each anchored to a picture.


1. A list, an array, and an index

Look at figure below: the strip has cells, and above each cell sits a small number — that number is its position, called an index. Programmers count indices starting at 0, so the first cell is index , the second is index , and so on.

Figure — Merge sort — divide and conquer, O(n log n), stable, proof of correctness

Why does the topic need this? Every step of merge sort talks about "the front element of a list" or "the box at index " — without indices we cannot point at which number we mean.


2. What "sorted" actually means

The symbol means "less than or equal to". Why or equal and not just "less than"? Because two boxes are allowed to hold the same number and the list is still sorted — think of two cards both showing sitting side by side. That tiny "or equal" is exactly what makes the topic's stability discussion possible later.

Figure — Merge sort — divide and conquer, O(n log n), stable, proof of correctness

3. Splitting: mid, //, and the floor/ceiling

The parent writes mid = len(a) // 2 and later and . Let us earn those.

Why do we need to drop the remainder? An index must be a whole number — there is no "box number ". So when we split a list of length , the cut lands at box , giving a left half of boxes and a right half of .

So for length : the left half has boxes, the right half has boxes. The two floor/ceiling brackets are just the precise way of saying "if it doesn't split evenly, the right side gets the extra box".

Figure — Merge sort — divide and conquer, O(n log n), stable, proof of correctness

4. Recursion — a function that calls itself

The parent's merge_sort calls merge_sort inside itself. That is recursion.

Why does the topic need this? "Divide and conquer" is recursion — the whole speed argument depends on the problem shrinking by half each call. See Recursion and Induction for the deeper machinery; the proof of correctness on the parent page leans on it directly.


5. Two pointers i, j — fingers walking a list

The merge routine walks both lists at once with these two fingers, always comparing the two boxes the fingers rest on and taking the smaller. This picture is the merge algorithm.

Figure — Merge sort — divide and conquer, O(n log n), stable, proof of correctness

6. Big-, , and — the size vocabulary

The parent says merge is and the whole sort is . Three symbols to unpack.

Why this exact tool and not another? Merge sort halves the list at every level. The natural question "how many levels of halving are there?" is precisely what a base-2 logarithm answers — that is why (and not, say, ) appears in the running time. The Master Theorem turns the recurrence into this automatically.

Recall Quick log check

::: , because is four halvings.


7. The recurrence symbols and the recursion tree

Why the topic needs this: the recurrence is the bridge from "the algorithm's shape" to "its running time". You solve it by drawing a recursion tree (each node splits into two half-size children), which the parent does to reach .


Prerequisite map

Array and index a of i

Sorted means neighbours in order

Splitting with integer div and floor ceil

Two pointers i and j

Merge sub-routine

Recursion and base case

Recurrence T of n

n and log and Big-O

Merge Sort


Equipment checklist

Test yourself — cover the right side and answer before revealing.

What does a[i] mean, and where does index counting start?
The value in the box at position ; counting starts at .
What is the difference between an index and a value?
Index = the box's address (where); value = what is stored inside it.
When is a list "sorted" in symbols?
When for every neighbouring pair.
Why and not in the definition of sorted?
Equal values are allowed side by side; the list is still sorted.
What is 7 // 2 and why?
— integer division floors (drops the remainder) because an index must be a whole number.
What do and do?
Round down and round up to the nearest whole number.
What is a base case in recursion?
The smallest input answered without recursing — here a list of length .
What is a pointer such as i?
A variable holding an index — a finger resting on one box.
What does count?
How many times you can halve before reaching .
What is the difference between and ?
is an upper bound (no faster than); is tight (grows exactly like).
What does stand for?
The time to sort items — a name whose formula we derive.

Connections

  • Merge Sort — parent topic — everything here feeds it.
  • Recursion and Induction — the self-calling machinery and the proof style.
  • Big-O Notation — the meaning of and .
  • Master Theorem — turns the recurrence into .
  • Divide and Conquer — the paradigm merge sort belongs to.
  • Stability of Sorting Algorithms — why the tie-rule matters.