See Merge sort run
Sort an array by divide and conquer. Sort each half, then merge the two sorted halves into one sorted array. Use the buttons or the keys: Space plays and pauses, the arrow keys step, Home and End jump.
MERGE-SORT(A, lo, hi) ▷ sort A[lo .. hi − 1]- 1
if hi − lo ≤ 1 then return - 2
mid ← lo + ⌊(hi − lo) / 2⌋ - 3
MERGE-SORT(A, lo, mid) - 4
MERGE-SORT(A, mid, hi) - 5
MERGE(A, lo, mid, hi) MERGE(A, lo, mid, hi) ▷ A[lo .. mid − 1] and A[mid .. hi − 1] are sorted- 6
B ← a copy of A[lo .. mid − 1] - 7
i ← 0, j ← mid, k ← lo - 8
while i < |B| and j < hi do - 9
if B[i] ≤ A[j] then A[k] ← B[i], i ← i + 1 ▷ ≤ keeps the sort stable - 10
else A[k] ← A[j], j ← j + 1 - 11
k ← k + 1 - 12
while i < |B| do - 13
A[k] ← B[i], i ← i + 1, k ← k + 1
Read the full page about Merge sort. To put this player on your site, use <iframe src="https://algorithm.tamnd.com/embed/merge-sort/" width="100%" height="520"></iframe>.