A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Merge Sort

College Depth 87 in the knowledge graph ☐ I know this ☆ Set as goal
5topics build on this
483prerequisites beneath it
See this on the map →
Arrays and ListsDivide and Conquer+4 more→→External Memory AlgorithmsParallel Algorithms and the PRAM Model+1 more
sorting merge-sort divide-and-conquer stable-sort

Core Idea

Merge sort is a divide-and-conquer algorithm that recursively splits an array into halves, sorts each half, and merges the sorted halves. The merge step, which combines two sorted arrays in O(n) time, is the core operation. The overall time complexity is O(n log n) in all cases, making it more predictable than quicksort. Merge sort is stable (equal elements retain their original order) and well-suited to linked lists and external sorting where data does not fit in memory.

How It's Best Learned

Implement the merge function first in isolation, then build the recursive mergeSort on top of it. Trace through an 8-element example drawing the full recursion tree and each merge step explicitly.

Common Misconceptions

Explainer

You already understand divide-and-conquer: break a problem into smaller subproblems, solve each recursively, and combine the results. Merge sort is the textbook application of this strategy to sorting. The key insight is that merging two already-sorted arrays into one sorted array is easy and fast — you just compare the front elements of each array and take the smaller one, repeating until both are exhausted. If you can produce sorted halves, combining them is an O(n) operation.

The algorithm works recursively. Given an array of n elements, split it into two halves of roughly equal size. Recursively sort the left half. Recursively sort the right half. Then merge the two sorted halves. The base case is an array of one element, which is trivially sorted. Picture the recursion as a tree: an 8-element array splits into two 4-element arrays, each splits into two 2-element arrays, each splits into two singletons. That's log₂(n) levels of splitting. At each level, the total merge work across all subproblems is O(n) — every element participates in exactly one merge at each level. So the total work is O(n log n), and this holds regardless of the input order. There is no "worst case" that degrades performance, unlike quicksort's O(n²) worst case.

The merge step requires a temporary array to hold the merged result, giving merge sort O(n) auxiliary space complexity. This is its main tradeoff compared to in-place sorts like quicksort or heapsort. However, merge sort has a crucial property those algorithms lack: stability. A stable sort preserves the relative order of elements with equal keys. If you sort a list of students by grade and two students both have a B+, they stay in whatever order they were in before the sort. This matters when sorting by multiple criteria — sort by name first, then by grade, and students with the same grade remain alphabetically ordered.

Merge sort also shines in two settings where other sorts struggle. For linked lists, the merge step can be done in-place by relinking pointers, eliminating the space overhead entirely. For external sorting — when data is too large to fit in memory — merge sort's sequential access pattern (reading and writing long runs) maps naturally onto disk I/O. You sort chunks that fit in memory, write them to disk, then merge the sorted chunks. This is why most database systems and many standard library sorts (like Python's Timsort, which is a hybrid merge-insertion sort) are built on merge sort's foundation.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Time and Space Complexity → Binary Search → Divide and Conquer → Merge Sort

Longest path: 88 steps · 483 total prerequisite topics

Prerequisites (6)

Leads To (3)