Merge Sort
Divide-and-conquer sorting that recursively splits and merges sorted subarrays.
The Mathematics
Divide-and-conquer paradigm, recurrence, and proof of correctness
Divide-and-Conquer Paradigm
Merge Sort breaks the problem into three steps:
Divide: Split the array into two halves at the midpoint .
Conquer: Recursively sort each half.
Combine: Merge the two sorted halves into a single sorted array.
Recurrence Relation
Each recursive call processes half the array. The merge step takes to combine two sorted halves:
Applying the Master Theorem (case 2: , so and ):
The Merge Operation
Given two sorted sequences and , the merge produces a sorted union by repeatedly taking the smaller head element:
Each element is compared at most once, so the merge takes time.
Correctness (by Induction)
Base case: An array of size is trivially sorted.
Inductive step: Assume correctly sorts arrays of size . Then for size :
Both halves are correctly sorted (by inductive hypothesis)
correctly combines two sorted sequences
Therefore the result is sorted
See It Work
Sorting [38, 27, 43, 3, 9, 82, 10] via recursive splitting and merging
Start with the unsorted array. We will recursively divide it in half.
The Code
Mapping divide-and-conquer to recursive Python
Mathematical Formulation
Base case: a single element is already sorted
Divide the array at the midpoint
Two recursive calls, each on half the array
Merge step: combine two sorted halves in linear time
Compare heads of both halves, take the smaller
Master theorem gives O(n log n) total
Python Implementation
def merge_sort(A):
if len(A) <= 1: # Base case
return A
mid = len(A) // 2 # Divide
left = merge_sort(A[:mid]) # Recurse left
right = merge_sort(A[mid:]) # Recurse right
return merge(left, right) # Conquer
def merge(L, R):
result = []
i = j = 0
while i < len(L) and j < len(R): # Compare heads
if L[i] <= R[j]:
result.append(L[i]) # Take from left
i += 1
else:
result.append(R[j]) # Take from right
j += 1
result.extend(L[i:]) # Remaining left
result.extend(R[j:]) # Remaining right
return result