An efficient, stable, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. Merge sort is a divide and conquer algorithm that was invented by John von Neumann in 1945. The algorithm splits the array into two halves, recursively sorts them, and then merges the two sorted halves.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
{
"arr": "[38,27,43,3,9,82,10]"
}Overview
Merge Sort is the classic divide-and-conquer sort. It splits the array in half, sorts each half recursively, and then merges the two sorted halves into one sorted run. An array of length one is already sorted, which ends the recursion.
All of the real work happens in the merge step. Two pointers walk the left and right halves; at each step the smaller front element is copied to the output. Because each level of recursion touches every element once and there are log2(n) levels, the total cost is O(n log n) for every input.
With the default input 38, 27, 43, 3, 9, 82, 10, the visualizer first drills down to single elements such as [38] and [27], then merges them back up: [27, 38], [3, 27, 38, 43], and finally the full sorted array.
Step by step
- If the current range has one element or fewer, return — it is sorted.
- Find the middle index
mand recursively sortl..mandm+1..r. - Copy the two halves into temporary arrays
LandR. - Repeatedly copy the smaller of
L[i]andR[j]back into the array, advancing that pointer. - When one half runs out, copy whatever remains of the other half.
Complexity
| Case | Cost | Why |
|---|---|---|
| Best case | O(n log n) | The split-and-merge structure does not depend on input order. |
| Average case | O(n log n) | log2(n) levels, each doing O(n) merge work. |
| Worst case | O(n log n) | Guaranteed — no bad pivot problem as in Quick Sort. |
| Extra space | O(n) | The temporary arrays used while merging. |
When to use it
- When you need a guaranteed O(n log n) and a stable sort, e.g. sorting records by a secondary key.
- Sorting linked lists, where merging needs no extra array and random access is unavailable.
- External sorting of data too large for memory: sort chunks, then merge them from disk.
Common mistakes
- Using
<instead of<=when comparingL[i]andR[j]takes equal elements from the right first and makes the sort unstable. - Computing the middle as
(l + r) / 2can overflow in fixed-width integer languages;l + (r - l) / 2is safe. - Allocating new temporary arrays on every call is a common performance trap; production code allocates one buffer up front.
A dry run is the best way to check your understanding of Merge Sort: follow each row, predict the next one, and compare. The run below takes 138 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.
- Input
- 38, 27, 43, 3, 9, 82, 10
- Result
- return value = [3, 9, 10, 27, 38, 43, 82]
| Step | Line | Array | What happens |
|---|---|---|---|
| 1 | 1 | [38, 27, 43, 3, 9, 82, 10] | |
| 2 | 2 | [38, 27, 43, 3, 9, 82, 10] | condition = 7 <= 1 |
| 6 | 2 | [38, 27, 43, 3, 9, 82, 10] | condition = 4 <= 1 |
| 10 | 2 | [38, 27, 43, 3, 9, 82, 10] | condition = 2 <= 1 |
| 14 | 2 | [38, 27, 43, 3, 9, 82, 10] | condition = 1 <= 1 |
| 18 | 2 | [38, 27, 43, 3, 9, 82, 10] | condition = 1 <= 1 |
| 23 | 13 | [38, 27, 43, 3, 9, 82, 10] | left = [38], right = [27], condition = 38 < 27 |
| 25 | 18 | [27, 27, 43, 3, 9, 82, 10] | resultArray = [27], rightIndex = 0 |
| 28 | 25 | [27, 38, 43, 3, 9, 82, 10] | resultArray = [27,38] |
| 31 | 2 | [27, 38, 43, 3, 9, 82, 10] | condition = 2 <= 1 |
| 35 | 2 | [27, 38, 43, 3, 9, 82, 10] | condition = 1 <= 1 |
| 39 | 2 | [27, 38, 43, 3, 9, 82, 10] | condition = 1 <= 1 |
| 44 | 13 | [27, 38, 43, 3, 9, 82, 10] | left = [43], right = [3], condition = 43 < 3 |
| 46 | 18 | [27, 38, 3, 3, 9, 82, 10] | resultArray = [3], rightIndex = 0 |
| 49 | 25 | [27, 38, 3, 43, 9, 82, 10] | resultArray = [3,43] |
| 53 | 13 | [27, 38, 3, 43, 9, 82, 10] | left = [27,38], right = [3,43], condition = 27 < 3 |
| 55 | 18 | [3, 38, 3, 43, 9, 82, 10] | resultArray = [3], rightIndex = 0 |
| 57 | 13 | [3, 38, 3, 43, 9, 82, 10] | left = [27,38], right = [3,43], condition = 27 < 43 |
| 59 | 15 | [3, 27, 3, 43, 9, 82, 10] | resultArray = [3,27], leftIndex = 0 |
| 61 | 13 | [3, 27, 3, 43, 9, 82, 10] | left = [27,38], right = [3,43], condition = 38 < 43 |
| 63 | 15 | [3, 27, 38, 43, 9, 82, 10] | resultArray = [3,27,38], leftIndex = 1 |
| 69 | 2 | [3, 27, 38, 43, 9, 82, 10] | condition = 3 <= 1 |
| 73 | 2 | [3, 27, 38, 43, 9, 82, 10] | condition = 2 <= 1 |
| 77 | 2 | [3, 27, 38, 43, 9, 82, 10] | condition = 1 <= 1 |
| 81 | 2 | [3, 27, 38, 43, 9, 82, 10] | condition = 1 <= 1 |
| 86 | 13 | [3, 27, 38, 43, 9, 82, 10] | left = [9], right = [82], condition = 9 < 82 |
| 94 | 2 | [3, 27, 38, 43, 9, 82, 10] | condition = 1 <= 1 |
| 99 | 13 | [3, 27, 38, 43, 9, 82, 10] | left = [9,82], right = [10], condition = 9 < 10 |
| 103 | 13 | [3, 27, 38, 43, 9, 82, 10] | left = [9,82], right = [10], condition = 82 < 10 |
| 105 | 18 | [3, 27, 38, 43, 9, 10, 10] | resultArray = [9,10], rightIndex = 0 |
| 108 | 25 | [3, 27, 38, 43, 9, 10, 82] | resultArray = [9,10,82] |
| 112 | 13 | [3, 27, 38, 43, 9, 10, 82] | left = [3,27,38,43], right = [9,10,82], condition = 3 < 9 |
| 116 | 13 | [3, 27, 38, 43, 9, 10, 82] | left = [3,27,38,43], right = [9,10,82], condition = 27 < 9 |
| 118 | 18 | [3, 9, 38, 43, 9, 10, 82] | resultArray = [3,9], rightIndex = 0 |
| 120 | 13 | [3, 9, 38, 43, 9, 10, 82] | left = [3,27,38,43], right = [9,10,82], condition = 27 < 10 |
| 122 | 18 | [3, 9, 10, 43, 9, 10, 82] | resultArray = [3,9,10], rightIndex = 1 |
| 124 | 13 | [3, 9, 10, 43, 9, 10, 82] | left = [3,27,38,43], right = [9,10,82], condition = 27 < 82 |
| 126 | 15 | [3, 9, 10, 27, 9, 10, 82] | resultArray = [3,9,10,27], leftIndex = 1 |
| 128 | 13 | [3, 9, 10, 27, 9, 10, 82] | left = [3,27,38,43], right = [9,10,82], condition = 38 < 82 |
| 130 | 15 | [3, 9, 10, 27, 38, 10, 82] | resultArray = [3,9,10,27,38], leftIndex = 2 |
The remaining 3 key steps follow the same pattern. Press play in the visualizer to watch every step through to the result.