Merge Sort explained step-by-step: divide-and-conquer recursion tree, linear merge subroutine, guaranteed O(n log n) performance, stability, and clean JavaScript implementation.
Merge Sort is a classic divide-and-conquer algorithm invented by John von Neumann in 1945. It breaks a problem down into smaller subproblems, solves them recursively, and combines the results. Crucially, Merge Sort guarantees O(n log n) time complexity across all cases (best, worst, and average), making it immune to pathological inputs.
Because Merge Sort is stable and accesses data sequentially rather than randomly, it is the standard algorithm of choice for sorting linked lists (where merging requires O(1) auxiliary pointer operations) and large datasets stored on disk (external merge sort).
If the array has 1 or 0 elements, it is already sorted (base case). Otherwise, find the midpoint and split the array into left and right halves.
Recursively call Merge Sort on the left subarray and right subarray until all base cases are reached.
Maintain two pointers at the start of the sorted left and right halves. Sequentially pick the smaller element and append it to the merged array.
Once one sub-array is exhausted, directly append all remaining elements from the other sub-array.
1function mergeSort(arr) {2 // Base case: arrays of length 0 or 1 are already sorted3 if (arr.length <= 1) return arr;4 5 const mid = Math.floor(arr.length / 2);6 const left = mergeSort(arr.slice(0, mid));7 const right = mergeSort(arr.slice(mid));8 9 return merge(left, right);10}11 12function merge(left, right) {13 const result = [];14 let l = 0;15 let r = 0;16 17 // Compare elements from left and right arrays18 while (l < left.length && r < right.length) {19 if (left[l] <= right[r]) {20 result.push(left[l]);21 l++;22 } else {23 result.push(right[r]);24 r++;25 }26 }27 28 // Append any remaining elements29 while (l < left.length) {30 result.push(left[l]);31 l++;32 }33 while (r < right.length) {34 result.push(right[r]);35 r++;36 }37 38 return result;39}40 41// Example usage:42// mergeSort([38, 27, 43, 3, 9, 82, 10]); // [3, 9, 10, 27, 38, 43, 82]Play through every comparison, swap, and state change, adjust the speed, or enter custom inputs to test edge cases.
The full interactive roadmap is unlocking — these specialized modules land in upcoming releases.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
This feature will be implemented in the next update.
Explore the full catalog — 30+ algorithms with more unlocking every release.
Ready — press Play or use step controls to walk through the algorithm.
Comparisons
0
Swaps / Shifts
0
Passes / Depth
0
Elements
10
Test your understanding of Merge Sort complexity and step mechanics.
Q1.What guarantees Merge Sort an O(n log n) worst-case time bound?
Q2.Why is Merge Sort particularly well-suited for sorting linked lists compared to Quick Sort?
Apply Merge Sort to real coding interview questions.
Given the head of a linked list, return the list after sorting it in O(n log n) time.