Merge Sort
A stable divide-and-conquer algorithm with consistent performance: split the array until indivisible, then merge back while sorting.
01Merge Sort
Explore How It WorksStarting merge sort. Split the array in half until it can't be divided further.
Split the array into two halves: 38 · 27 · 43 and 10 · 76 · 15.
Split the left half again into 38 · 27 and 43.
Split 38 · 27 into single values as well.
38 stands alone, so it is already sorted.
So is 27. Nothing left to split — time to merge.
Merge the 38 and 27 we just separated.
Compare the two fronts, 38 and 27. 27 is smaller, so it goes out first.
Put 27 down in the first slot.
One side is empty, so append the remaining 38 as is.
43, left on its own, is sorted too.
Merge 43 into the sorted 27 · 38 to complete the left half.
Compare the fronts, 27 and 43. 27 is smaller.
Put 27 down in the first slot.
Next up, 38 and 43. The left side is smaller again.
Put 38 down in the next slot.
The left side is empty, so append the remaining 43. The left half now reads 27 · 38 · 43.
Now the right half. Split it into 10 · 76 and 15.
Split 10 · 76 into single values as well.
10 can't be divided further.
Neither can 76. Time to merge the two.
Merge the 10 and 76 we just separated.
Compare the fronts, 10 and 76. 10 is smaller.
Put 10 down in the first slot.
Append the remaining 76.
15, left on its own, is sorted too.
Merge 15 into the sorted 10 · 76 to complete the right half.
Compare the fronts, 10 and 15. 10 is smaller.
Put 10 down in the first slot.
Next up, 76 and 15. This time the right side is smaller.
Put 15 down in the next slot.
Append the remaining 76. The right half now reads 10 · 15 · 76.
Merge the two sorted halves one last time. Only the fronts ever need comparing.
Compare 27 and 10. The right side is smaller.
Put 10 down in the very first slot.
Compare 27 and 15. The right side is smaller again.
Put 15 down in the next slot.
Compare 27 and 76. This time the left side is smaller.
Put 27 down in the next slot.
Compare 38 and 76. The left side is still smaller.
Put 38 down in the next slot.
Compare 43 and 76. The last value on the left is smaller.
Put 43 down in the next slot.
The left side is exhausted. Put the remaining 76 at the very end.
Merge sort is done. The order is decided as the pieces come back together, not as they are split.
02 Understand It Simply
For EveryoneHalves the input repeatedly, then merges sorted pieces by comparing their fronts. O(n log n) on any input.
Splits the array in halves, sorts each, and merges two sorted pieces in order.
Always O(n log n) and stable.
- –Sorting that needs stability
- –external sorting of large files
03 Python Implementation
A clean, readable reference implementation of the core logic of Merge Sort.
04 Frequently Asked Questions
FAQWhat is Merge Sort?+
A stable divide-and-conquer algorithm with consistent performance: split the array until indivisible, then merge back while sorting.
What is the time complexity of Merge Sort?+
The time complexity of Merge Sort is O(n log n). Follow the step-by-step visualization to see exactly why.
Where is Merge Sort used?+
Sorting that needs stability, external sorting of large files.
What's a simple analogy for Merge Sort?+
Halves the input repeatedly, then merges sorted pieces by comparing their fronts. O(n log n) on any input.
