Skip to content
Part IA Lent Term

Mergesort

Algorithm

Mergesort is a classic divide-and-conquer sorting algorithm:

  1. Divide: Split the array into two halves of (roughly) equal size.
  2. Conquer: Recursively sort each half.
  3. Combine: Merge the two sorted halves into a single sorted array.

Mergesort: divide array into halves, recursively sort, then merge

Merge Procedure

Given two sorted subarrays LL and RR, use two pointers ii and jj:

  1. Initialise i=1i=1, j=1j=1 (pointing to the start of each subarray).
  2. For k=1k = 1 to L+R|L| + |R|:
  3.  If L[i]R[j]L[i] \le R[j]: place L[i]L[i] into output, increment ii.
  4.  Else: place R[j]R[j] into output, increment jj.
  5. If one subarray is exhausted, copy the remainder of the other.

A common trick: append a sentinel \infty to both subarrays so neither is ever exhausted before the loop ends.

Walkthrough Example

Input: [9,102,10,7,64,18][9, 102, 10, -7, 64, 18]

Split:                [9, 10, 102, -7, 64, 18]
                     /                          \
           [9, 10, 102]                    [-7, 64, 18]
          /            \                  /             \
     [9]                [10, 102]     [-7]              [64, 18]
   (base)             /       \     (base)            /        \
                 [10]          [102]              [64]          [18]
                (base)        (base)            (base)        (base)

Merge up: [9] + [10, 102] -> [9, 10, 102]
          [64] + [18] -> [18, 64]   (sorted merge)
          [-7] + [18, 64] -> [-7, 18, 64]
          [9, 10, 102] + [-7, 18, 64] -> [-7, 9, 10, 18, 64, 102]

Analysis

Recurrence

Let T(n)T(n) be the cost of sorting nn elements:

T(n)={Θ(1)if n=12T(n/2)+Θ(n)if n>1T(n) = \begin{cases} \Theta(1) & \text{if } n = 1 \\ 2T(n/2) + \Theta(n) & \text{if } n > 1 \end{cases}

The Θ(n)\Theta(n) term covers both the array copying for the subarrays and the merge comparison loop.

Closed Form via Substitution

Unwinding the recurrence:

T(n)=2T(n/2)+kn=2[2T(n/4)+kn/2]+kn=4T(n/4)+2knT(n) = 2T(n/2) + kn = 2[2T(n/4) + k n/2] + kn = 4T(n/4) + 2kn

After log2n\log_2 n substitutions:

T(n)=nT(1)+knlog2n=Θ(nlogn)T(n) = nT(1) + kn \log_2 n = \Theta(n \log n)

Via Master Theorem

a=2a = 2, b=2b = 2, f(n)=Θ(n)f(n) = \Theta(n). Compute nlogba=nlog22=nn^{\log_b a} = n^{\log_2 2} = n. Since f(n)=Θ(nlogba)f(n) = \Theta(n^{\log_b a}), Case 2 applies:

T(n)=Θ(nlogbalogn)=Θ(nlogn)T(n) = \Theta(n^{\log_b a} \cdot \log n) = \Theta(n \log n)

The Call Tree Method

At the top level, merge costs knkn. At the next level, two merges each cost kn/2k n/2, totalling knkn. Each of the log2n+1\log_2 n + 1 levels contributes exactly knkn, so T(n)=kn(log2n+1)=Θ(nlogn)T(n) = kn(\log_2 n + 1) = \Theta(n \log n).

Properties

PropertyValue
Worst caseΘ(nlogn)\Theta(n \log n)
Average caseΘ(nlogn)\Theta(n \log n)
Best caseΘ(nlogn)\Theta(n \log n)
StableYes — merge preserves order of equal keys
In-placeNo (standard); Θ(n)\Theta(n) extra space
Data-sensitiveNo — always Θ(nlogn)\Theta(n \log n)
Non-integer nnT(n)=T(n/2)+T(n/2)+Θ(n)T(n) = T(\lceil n/2 \rceil) + T(\lfloor n/2 \rfloor) + \Theta(n), same solution

Variations

  • Bottom-up merge sort: Iterative, merging pairs of size 1, then 2, then 4, etc. Useful for linked lists where splitting is O(1)O(1).
  • Natural merge sort: Exploits existing runs (sorted subsequences) in the input; can approach O(n)O(n) on nearly-sorted data.
  • In-place merge sort: The merge step can be made in-place with clever data movement, but the constant factor becomes large and practical implementations use the extra space.

Summary

AspectDetail
StrategyDivide and conquer
RecurrenceT(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n)
SolutionΘ(nlogn)\Theta(n \log n)
Master TheoremCase 2: a=2,b=2,f(n)=Θ(n)a=2, b=2, f(n)=\Theta(n)
SpaceΘ(n)\Theta(n) extra
StableYes
OptimalYes — matches Ω(nlogn)\Omega(n \log n) lower bound