Merge Sort
Legend
- • Numbers show element values
- • Bottom labels show array indices
- • Colors indicate current operation
- • Height represents value magnitude
Inspector
Legend
- • Numbers show element values
- • Bottom labels show array indices
- • Colors indicate current operation
- • Height represents value magnitude
Inspector
Reference implementation of Merge Sort. Select a language tab to view and copy the code.
#include <string.h>
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1, n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void merge_sort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}#include <vector>
void merge(std::vector<int>& arr, int left, int mid, int right) {
std::vector<int> L(arr.begin() + left, arr.begin() + mid + 1);
std::vector<int> R(arr.begin() + mid + 1, arr.begin() + right + 1);
int i = 0, j = 0, k = left;
while (i < (int)L.size() && j < (int)R.size())
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < (int)L.size()) arr[k++] = L[i++];
while (j < (int)R.size()) arr[k++] = R[j++];
}
void mergeSort(std::vector<int>& arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
private static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1, n2 = right - mid;
int[] L = Arrays.copyOfRange(arr, left, mid + 1);
int[] R = Arrays.copyOfRange(arr, mid + 1, right + 1);
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}def merge_sort(arr: list[int]) -> None:
if len(arr) <= 1:
return
mid = len(arr) // 2
left, right = arr[:mid], arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
arr[k] = left[i]; i += 1
else:
arr[k] = right[j]; j += 1
k += 1
while i < len(left):
arr[k] = left[i]; i += 1; k += 1
while j < len(right):
arr[k] = right[j]; j += 1; k += 1function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
function merge(left, right) {
const result = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
result.push(left[i] <= right[j] ? left[i++] : right[j++]);
}
return result.concat(left.slice(i)).concat(right.slice(j));
}Python — Code Walkthrough
In-place variant using slices
left, right = arr[:mid], arr[mid:]Python slice notation creates new list copies for the left and right halves. The function sorts them recursively then merges back into the original arr by index assignment.
Base case — len <= 1
if len(arr) <= 1: returnA list of 0 or 1 elements is already sorted; return immediately. This is the recursion termination condition.
Three-pointer merge
i = j = k = 0i scans the left half, j scans the right half, and k tracks the write position in arr. All three advance in a coordinated merge loop.
Stable comparison
if left[i] <= right[j]: arr[k] = left[i]; i += 1Taking from the left half when equal keeps the sort stable. Python multi-statement lines use semicolons here for compactness, though separate lines are more idiomatic.
Drain remaining elements
while i < len(left): arr[k] = left[i]; i += 1; k += 1After one half is exhausted, the other is already in order — copy the rest directly. At most one of the two drain loops will run.