DrawCode Algorithm Visualizer • Sorting • Medium

Merge Sort

Tags: Sorting, Divide and Conquer, Stable, Merge

1. One-Liner

Merge Sort splits the array in half, sorts each half recursively, and merges the two sorted halves into one sorted output.


2. The Problem It Solves

You need guaranteed O(n log n) time for arbitrary inputs and stability (equal keys keep their relative order) — common in external sorting and as a building block of Tim Sort.


3. The Core Idea

Two piles of playing cards are already sorted face-up. Repeatedly take the smaller top card from either pile — the merged pile is sorted.


4. How It Works (Step-by-Step)

StepWhat Happens
1.Base case: length ≤ 1 → already sorted
2.Split at midpoint mid
3.Recursively sort left [0, mid) and right [mid, n)
4.Merge: two pointers; always take the smaller head (left on ties for stability)
5.Append leftover elements from the longer tail

5. Dry Run Example

Merge [1, 4] and [2, 3]: output 1, 2, 3, 4 — each step picks the minimum of the two fronts.


6. Key Properties

PropertyValue
StableYes if left chunk is preferred when equal
Extra spaceO(n) for typical array merge
ParallelLeft/right sorts are independent

7. Where It Is Used

Company / SystemHow They Use It
Java / Python TimSortMerge runs of ordered data
External sortLarge files on disk in chunks

8. Interview Tips

Be fluent in the merge routine; explain why total work is O(n log n) via recursion tree; compare to Quick Sort (in-place vs stable).


9. Comparison with Other Algorithms

AlgorithmWorst timeExtra spaceStable
MergeO(n log n)O(n)Yes
QuickO()O(log n) stackNo
HeapO(n log n)O(1)No

10. Complexity

MetricValue
Time (Best)O(n log n)
Time (Average)O(n log n)
Time (Worst)O(n log n)
SpaceO(n) auxiliary (typical array merge)
Stable?Yes

Implementation Example (PYTHON)

def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)

def merge(a, b):
    i, j, out = 0, 0, []
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            out.append(a[i]); i += 1
        else:
            out.append(b[j]); j += 1
    out.extend(a[i:]); out.extend(b[j:])
    return out

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer