Beginner Topic 11 of 51 Algorithms
Merge Sort
Divide-and-conquer sort that splits and merges halves.
How it works
Merge sort splits the array into halves, recursively sorts each half and merges the two sorted halves by repeatedly taking the smaller front element. It guarantees O(n log n) time in every case, is stable and parallelizes well, but the standard array version needs O(n) extra space for merging. It follows the divide-and-conquer pattern directly.
Where it is used
Sorting linked lists, external sorting of files too large for memory, stable sorting of records, counting inversions and the library sort in languages like Java for objects use merge sort.
Common pitfall
The extra buffer costs memory and copying time, so it is often slower than quicksort on arrays in practice. Allocating a new array in each recursive call is wasteful; reuse one buffer. Merging with the wrong comparison breaks stability. Switch to insertion sort for tiny sub-arrays to speed it up.
Where it fits in the course
Merge Sort is topic 11 of 51 in Algorithms, rated Beginner. It follows Insertion Sort. The next topic is Quick Sort.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the Sorting Algorithms visualizer: Bubble, selection, insertion, merge and quick sort animated.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Merge Sort?
Where do people use Merge Sort?
What is the most common mistake with Merge Sort?
What should I learn before this topic?
How can I practice it?
Is Merge Sort hard to learn?
How long does it take to learn Merge Sort?
Which topics are related to Merge Sort?
What should I study after Merge Sort?
Related topics
- Insertion Sort (Algorithms)
- Quick Sort (Algorithms)
- Selection Sort (Algorithms)
- Counting Sort (Algorithms)
- Resolving Merge Conflicts (DevOps & Tooling)