AiTechWorlds
AiTechWorlds
Watch Merge Sort divide and merge with guaranteed O(n log n) behaviour, synced pseudocode, and multi-language code.
def merge_sort(a):
if len(a) <= 1:
return a
mid = len(a) // 2
L, R = merge_sort(a[:mid]), merge_sort(a[mid:])
out, i, j = [], 0, 0
while i < len(L) and j < len(R):
if L[i] <= R[j]: out.append(L[i]); i += 1
else: out.append(R[j]); j += 1
return out + L[i:] + R[j:]Merge Sort recursively splits the array in half, sorts each half, then merges them back together — guaranteeing O(n log n) in every case.
| Best | O(n log n) |
| Average | O(n log n) |
| Worst | O(n log n) |
| Space | O(n) |
| Stable | Yes |
| In-place | No |
The Merge Sort Visualizer animates how Merge Sort works: it recursively splits the array in half, sorts each half, then merges the sorted halves back together. Merge Sort guarantees O(n log n) time in the best, average, and worst case, uses O(n) extra space, and is stable. Use Play, Step, and the timeline to watch the merge step compare and overwrite values.
Generate an array
Click New Array or adjust the size slider for a random dataset.
Play the animation
Press Play to watch subarrays merge; overwritten positions are highlighted.
Step and scrub
Use Step Forward/Back or the timeline to study each merge comparison.
Read the synced code
The highlighted pseudocode line follows the merge; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.