Merge Sort
Algorithm · Core
Watch it run
Step 1 of 12
Merge sort: split in half, sort each half, then merge two sorted halves into one.
Complexity
| Name | Best | Average | Worst | Memory | Stable | Comments |
|---|---|---|---|---|---|---|
| Merge sort | n log(n) | n log(n) | n log(n) | n | Yes |
In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged.
A recursive merge sort algorithm used to sort an array of 7 integer values. These are the steps a human would take to emulate merge sort (top-down).
Code
import Sort from '../Sort';
export default class MergeSort extends Sort {
sort(originalArray) {
// Call visiting callback.
this.callbacks.visitingCallback(null);
// If array is empty or consists of one element then return this array since it is sorted.
if (originalArray.length <= 1) {
return originalArray;
}
// Split array on two halves.
const middleIndex = Math.floor(originalArray.length / 2);
const leftArray = originalArray.slice(0, middleIndex);
const rightArray = originalArray.slice(middleIndex, originalArray.length);
// Sort two halves of split array
const leftSortedArray = this.sort(leftArray);
const rightSortedArray = this.sort(rightArray);
// Merge two sorted arrays into one.
return this.mergeSortedArrays(leftSortedArray, rightSortedArray);
}
mergeSortedArrays(leftArray, rightArray) {
const sortedArray = [];
// Use array pointers to exclude old elements after they have been added to the sorted array.
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < leftArray.length && rightIndex < rightArray.length) {
let minElement = null;
// Find the minimum element between the left and right array.
if (this.comparator.lessThanOrEqual(leftArray[leftIndex], rightArray[rightIndex])) {
minElement = leftArray[leftIndex];
// Increment index pointer to the right
leftIndex += 1;
} else {
minElement = rightArray[rightIndex];
// Increment index pointer to the right
rightIndex += 1;
}
// Add the minimum element to the sorted array.
sortedArray.push(minElement);
// Call visiting callback.
this.callbacks.visitingCallback(minElement);
}
// There will be elements remaining from either the left OR the right
// Concatenate the remaining elements into the sorted array
return sortedArray
.concat(leftArray.slice(leftIndex))
.concat(rightArray.slice(rightIndex));
}
}References
Practice
Divide & Conquer
- Majority ElementEasy
- Convert Sorted Array to Binary Search TreeEasy
- Pow(x, n)Medium
- Sort ListMedium
- Maximum SubarrayMedium
- Different Ways to Add ParenthesesMedium
- Construct Quad TreeMedium
- Kth Largest Element in an ArrayMedium
- Merge k Sorted ListsHard
- Reverse PairsHard
- The Skyline ProblemHard
- Median of Two Sorted ArraysHard
Spotted a mistake or something unclear? Report it on GitHub.