DrawCode Algorithm Visualizer • Divide and Conquer • Medium

Merge Sort (Divide & Conquer)

Tags: Divide and Conquer, Sorting, Stable, Merge

1. One-Liner

Merge sort splits the array into two halves, sorts each half recursively, and merges two sorted sequences into one sorted array.


2. The Problem It Solves

You need a predictable O(n log n) comparison sort with stable merging behavior—common in external sorting, linked lists, and parallel pipelines where divide-and-conquer maps cleanly to tasks.


3. The Core Idea

Divide the problem into two equal subproblems, conquer them independently, then combine with a linear merge that walks two pointers—like zipping two sorted decks of cards.


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

StepWhat Happens
1. Base caseIf length ≤ 1, the segment is already sorted.
2. SplitFind mid index; recurse on [lo, mid) and [mid, hi).
3. MergeTwo-pointer merge from temporary left/right buffers into the original range.
4. StabilityWhen keys tie, take from the left run first to preserve order.

5. Dry Run Example

Array: [38, 27, 43, 3]

PhaseResult
Split & sort leaves[27,38] and [3,43]
Merge pairs[27,38] and [3,43]
Final merge[3,27,38,43]

6. Key Properties

PropertyValue
TimeO(n log n) all cases (typical merge)
SpaceO(n) auxiliary for merge buffers
StableYes (with ≤ merge rule)
AdaptiveNot inherently adaptive

7. Where It Is Used

DomainUse
Databases / filesExternal merge sort on large runs
Functional libsImmutable list sorting via merge
Parallel systemsFork sub-sorts, merge results

8. Interview Tips

State recurrence T(n)=2T(n/2)+O(n)O(n log n), mention stable merge, contrast space vs quicksort’s in-place average case.


9. Comparison with Other Algorithms

AlgorithmTime (worst)SpaceStable
Merge sortO(n log n)O(n)Yes
Quick sortO()O(log n) stackNo
Heap sortO(n log n)O(1)No

10. Complexity

MetricValue
Time (Best/Avg/Worst)O(n log n)
SpaceO(n) auxiliary
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 = 0
    out = []
    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
    return out + a[i:] + b[j:]

Interactive Visualizer Workspace

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

Launch Interactive Visualizer