Searching & Trees · 9 min read · Intermediate
Sorting: Merge Sort & O(n log n) Trade-Offs
Understand Divide & Conquer sorting with Merge Sort, stability, and why comparison sorts have an O(n log n) lower bound.
What you'll learn
- Implement Divide & Conquer Merge Sort in O(n log n) guaranteed time
- Explain what a stable sort preserves when two keys are equal
- Compare Merge Sort, Quick Sort, and Timsort trade-offs
Merge Sort divides an array in half recursively until subarrays have length 1, then merges sorted halves back together in linear time. Unlike Quick Sort (which can degrade to $O(n^2)$ on bad pivots), Merge Sort guarantees $O(n \log n)$ worst-case time and is stable (preserving original relative order for equal keys).
Fill-in-the-Blank Exercise
Merge Sort Base Case & Midpoint
Return immediately when length <= 1 and split at the midpoint.
if (arr.length <= ) return arr;
const mid = Math.floor(arr.length / );Live DSA Playground
Divide & Conquer Merge Sort
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. What is the worst-case time complexity of Merge Sort?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.