Merge Sort
One array, traced the whole way down and the whole way back — because the objective asks for the result of each iteration.
- Merge sort is a recursive sorting algorithm that can sort elements in an array or ArrayList.
- It repeatedly divides an array into smaller subarrays until each subarray is one element, then merges them back in sorted order.
- Each call sorts its left half completely (dividing and merging), then its right half, then merges the two.
- Split at the middle with
mid = (low + high) / 2; integer division gives the left half the extra element. - Dividing stops at a single element because a single element is already in sorted order.
- Merge order: the recursion finishes the whole left half completely before the right side is touched at all.
- Inside a merge, take the smaller front element; when one side runs out, copy everything left in the other side.
- Every merge assumes both of its inputs are already in sorted order — true for free at the bottom.
- Sorting n elements always takes n − 1 merges: six elements, five merges.
1What the Objective Actually Asks
4.17.C — determine the result of each iteration of the merge sort algorithm when used to sort a collection. Not “explain merge sort”, and not “write merge sort”. You are asked what the collection looks like at a particular point part-way through, which means you have to be able to walk it.
Merge sort is a recursive sorting algorithm that can be used to sort
elements in an array or ArrayList.
Merge sort repeatedly divides an array into smaller subarrays until each subarray is one element, and then recursively merges the sorted subarrays back together in sorted order to form the final sorted array.
That second statement is the whole algorithm in one sentence: divide down to single elements, then merge back up. In the actual run the two are interleaved: each call divides and fully merges its left half before it touches its right half.
1.1 You Trace It; You Never Write It
Merge sort is recursive, and the course excludes writing recursive code (the exclusion sits on 4.16.A, Recursive Method Calls). Sorting algorithms other than selection, insertion and merge sort are excluded too.
Reading a merge sort, and saying what the array holds after a given number of merges. That is a tracing skill, not an authoring skill — and it is the one the objective names.
2The Array We Will Sort All the Way Through
int[] data = {5, 2, 9, 1, 7, 3};
Six elements, deliberately not a power of two, because uneven splits are where traces go wrong. Every figure below uses this same array so the whole sort is one continuous story.
3Phase One — Divide Until Every Piece Is One Element
Each call splits its range at the middle index and hands the two halves to itself. With
low and high as the ends of the range:
int mid = (low + high) / 2; // integer division — the left half takes the extra element
For low = 0, high = 5, mid is
2, so the left range is 0–2 and the right
is 3–5 — three elements each. For a five-element range the left half would get the
extra one. Integer division decides it, and there is nothing to remember beyond that.
3.1 “Until Each Subarray Is One Element”
The dividing stops when a range holds a single element, because a single element is already in sorted order — there is nothing to compare it to. That is the base case, and it is why the merging has something sorted to start from.
4Phase Two — Merge Back Up
A merge takes two already sorted pieces and produces one sorted piece. It never searches and never re-sorts; it just repeatedly takes the smaller of the two front elements.
4.1 The Order the Merges Actually Happen In
This is the part traces get wrong. The merges do not alternate between the two halves. The recursion goes all the way down the left side and finishes it completely before the right side is touched at all:
| # | Merge | Result | The array so far |
|---|---|---|---|
| 1 | [5] and [2] | [2, 5] | 2 5 9 | 1 7 3 |
| 2 | [2, 5] and [9] | [2, 5, 9] | 2 5 9 | 1 7 3 — left half done |
| 3 | [1] and [7] | [1, 7] | 2 5 9 | 1 7 3 |
| 4 | [1, 7] and [3] | [1, 3, 7] | 2 5 9 | 1 3 7 — right half done |
| 5 | [2, 5, 9] and [1, 3, 7] | [1, 2, 3, 5, 7, 9] | sorted |
“What does the array contain after the third merge?” The answer is
2 5 9 1 7 3 — the left half sorted, the right half barely
started. A student who assumes the merges alternate will have the two halves progressing
together and will get every intermediate question wrong while still producing the right final
answer.
5Inside a Single Merge
Every merge works the same way, so it is worth doing one slowly. The final merge combines
[2, 5, 9] with [1, 3, 7]:
Look at the front of each piece. Take the smaller one and advance that side. Repeat. When one side runs out, copy everything left in the other side — it is already sorted, and it is already larger than everything taken so far.
5.1 Why the Last Step Is a Copy, Not a Comparison
Once one side is exhausted there is nothing to compare against, and nothing left to decide. The remaining elements go across in the order they are already in. Students often stop the trace at the last comparison and lose the tail.
6What Makes the Merge Cheap
A merge never looks backwards and never re-examines an element it has already placed. Each pass through it touches each element once. That is only possible because both inputs arrived sorted — which is exactly what the dividing phase guarantees by going all the way down to single elements.
Every merge in the whole algorithm assumes both of its inputs are already in sorted order. That assumption is true at the bottom for free, because a one-element piece is sorted, and every merge above it preserves it. If you can state that, you can explain why merge sort works without writing a line of it.
7Check Yourself
Using {5, 2, 9, 1, 7, 3}, what does the array hold after the
second merge?
2 5 9 1 7 3. The left half is fully sorted after merge 2, and
nothing on the right has been touched yet.
How many merges does sorting six elements take?
Five. Every merge reduces the number of separate sorted pieces by one, and six single-element pieces have to become one — so it always takes one fewer merge than there are elements.
Why does the dividing phase go all the way down to single elements rather than stopping at pairs?
Because a merge requires both inputs to be sorted already, and a single element is sorted without any work. Stopping at pairs would leave pieces that still needed sorting before the first merge could run.
In the final merge of [2, 5, 9] and [1, 3, 7],
how many comparisons happen?
Five. The sixth element needs no comparison — by then the right side is exhausted and
the remaining 9 is simply copied across.
Will you be asked to write a merge sort on the exam?
No. Merge sort is recursive and writing recursive code is outside the scope of the course. You are asked to determine the result of each iteration — to read one and trace it.
8Summary
| Idea | What to remember |
|---|---|
| What it is | A recursive sorting algorithm, for an array or an ArrayList |
| Phase one | Repeatedly divide until every subarray is one element |
| Phase two | Recursively merge the sorted subarrays back together in sorted order |
| The split | mid = (low + high) / 2 — integer division; the left half takes the extra |
| Merge order | The whole left half finishes before the right half starts |
| Inside a merge | Take the smaller front element; when one side empties, copy the rest |
| The invariant | Every merge assumes both inputs are already sorted — true for free at the bottom |
| Count | n elements take n − 1 merges |
| Scope | Trace it. Writing recursive code is excluded |
Merge sort makes every piece trivial before it sorts it — it splits down to one-element pieces, which are sorted by definition — and no merge ever does anything harder than picking the smaller of two front elements.