- Explain the divide, base-case, and merge phases of the recursion tree.
- Trace two sorted halves as they become one sorted output.
- Implement Merge Sort in JavaScript, Python, Java, or C++.
- Recognize its stable O(n log n) guarantee and O(n) auxiliary-space cost.
Divide and conquer, made visible
Split the problem.
Merge the answers.
Merge Sort keeps dividing an array until every piece is trivial, then combines those pieces in sorted order.
Key idea Two sorted halves can be merged in one linear scan by comparing only their front values.
Before you start
What you will learn
A clear finish line for the lesson, plus the small amount of knowledge you need before beginning.
- Arrays, slices, and indexes
- Functions and recursion basics
- Big O and logarithms from the glossary
01 — The idea
How Merge Sort thinks
Instead of sorting one large array directly, what if we solve two smaller versions and combine them?
Divide in half
Split each array near its midpoint until only single values remain.
Trust the small answers
A one-value array is already sorted, so recursion has a base case.
Merge in order
Compare the fronts of two sorted halves and take the smaller value.
02 — Try it yourself
Watch the recursion tree resolve
Follow each split from root to base case, then compare both fronts as sorted halves merge upward.
Turn each animation into a written decision
| Step | Pass | Action | Current fronts | Current output | Writes |
|---|---|---|---|---|---|
| 0 | — | Ready | — | [38, 27, 43, 3, 9, 82, 10, 15] | 0 |
The highlighted row always matches the visualization above. Step forward to extend the trace.
03 — Read the code
From recursive halves to working code
Compare the same base case, recursive split, and stable left-first merge across all four languages.
01function mergeSort(numbers) {02 if (numbers.length <= 1) return [...numbers];03 const middle = Math.floor(numbers.length / 2);04 const left = mergeSort(numbers.slice(0, middle));05 const right = mergeSort(numbers.slice(middle));06 return merge(left, right);07}08 09function merge(left, right) {10 const output = [];11 let leftIndex = 0;12 let rightIndex = 0;13 while (leftIndex < left.length && rightIndex < right.length) {14 if (left[leftIndex] <= right[rightIndex]) {15 output.push(left[leftIndex++]);16 } else {17 output.push(right[rightIndex++]);18 }19 }20 return output.concat(left.slice(leftIndex), right.slice(rightIndex));21}04 — Avoid the traps
Common mistakes
These bugs often produce an answer that looks almost right. Learn the symptom and the correction together.
Missing the base case
Symptom: The recursive calls continue forever or fail on empty arrays.
Correction: Return a copy immediately when the array has zero or one value.
Dropping remaining values
Symptom: The merged output is shorter than the two inputs combined.
Correction: After one half is exhausted, append every value still waiting in the other half.
Taking from the right on ties
Symptom: Equal values reverse their original order.
Correction: Use ≤ and take the left value first when both fronts are equal.
Re-sorting during merge
Symptom: The merge step uses another sorting function or scans the full output repeatedly.
Correction: Rely on both halves already being sorted and advance one front pointer at a time.
05 — Check yourself
Check your understanding
Answer 4 questions, then reveal the reasoning. Your progress stays on this device—no account required.
Answer every question to check your work.
06 — Mini game
Merge two sorted queues
Build one sorted output by repeatedly choosing the smaller front value.
Compare only the two available front cards.
07 — Use it wisely
Where Merge Sort fits
Predictable performance
O(n log n) time is guaranteed regardless of the input's original order.
Great fitExternal sorting
Sequential merging works well when large datasets live on disk instead of memory.
Excellent fitLinked lists and parallel work
Halves can be processed independently, and linked lists merge without shifting values.
Good fitTiny memory-limited arrays
Temporary merge storage and recursive overhead may outweigh its benefits.
Consider another sortLesson progress
Finish this lesson with confidence
Use this private checklist to confirm what you learned. Progress stays only in this browser.
Complete every checklist item to finish the lesson.