Merge buffer (the temp array)
Merge sort recursively splits the array in half, then merges sorted halves.
Press play — this strip shows two halves draining into one buffer.
Array (current state)
42
0
17
1
88
2
33
3
71
4
9
5
56
6
25
7
64
8
12
9
AlgoPlus//sorting / merge sort
Read the theory

Merge Sort · DNA Synchronization

Two sorted strands braid together into one perfectly ordered sequence.

Stability
Stable
In-Place
No
Space Complexity
O(n)
Avg Time
O(n log n)
Size10
Legend
Comparing
Swapping / moving
Tracked (min / key)
Pivot
Sorted / found
Out of focus
AI Tutor Workspace
In a nutshell
Merge sort splits the list in half again and again until each piece holds one item, then merges pairs of pieces back together in order. Merging two already-sorted lists is easy: keep taking the smaller front item. It guarantees O(n log n) speed on any input, but needs extra scratch memory to do the merging.
Ready
Press play to begin the cinematic walkthrough.
Divide the array in halves recursively, then merge two sorted halves like braiding DNA strands.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Merge Sort · DNA Synchronization.
Code Simulator
1
def merge_sort(arr, l, r):
2
    if l < r:
3
        mid = (l + r) // 2
4
        merge_sort(arr, l, mid)
5
        merge_sort(arr, mid + 1, r)
6
        merge(arr, l, mid, r)
7
def merge(arr, l, mid, r):
8
    # merge two sorted halves
9
    arr[k] = temp[i]  # overwrite
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.