Merge sort is the classic divide and conquer sort. It guarantees n log n time whatever the input.

The Algorithm

Merge sort divides the array into two halves, sorts each half recursively and merges the two sorted halves into one sorted array. An array of one element is already sorted and forms the base case. The real work is done in the merge step: two pointers walk down the two sorted halves, always copying the smaller current element into an auxiliary array, until one half is finished, and then the rest of the other half is copied. Merging two lists of total size n needs at most n − 1 comparisons and exactly n copies, so it is Θ(n). Taking the left element when keys are equal makes the sort stable.

Analysis

Sorting n elements means sorting two halves of n/2 and merging in time proportional to n, so T(n) = 2T(n/2) + n. By the Master theorem (a = 2, b = 2, f(n) = n = n^(log₂ 2)), Case 2 gives T(n) = Θ(n log n). Equivalently, the recursion tree has log₂ n levels and each level performs about n work in merging. The best, average and worst cases are all Θ(n log n); the best case needs about (n log n)/2 comparisons when the input is already sorted, the worst about n log n − n + 1. For n = 1,000,000 this is roughly 20 million comparisons, compared with about 500 billion for an n² sort.

Space and Practical Notes

Merge sort needs an auxiliary array of size n, so its space complexity is Θ(n) and it is not in place. The recursion stack adds O(log n). It performs well on linked lists (merging needs no extra array) and for external sorting of data too large for memory, because it reads the data sequentially. For small arrays a simple sort like insertion sort can be used for the base case to reduce overhead.

Definition: Merge sort. A stable divide and conquer sorting algorithm that splits the array in halves, sorts each recursively and merges the sorted halves, taking Θ(n log n) time and Θ(n) extra space.

Table 2.2: Merge sort at a glance

Property Value Remark
Time (all cases) Θ(n log n) Independent of input order
Extra space Θ(n) Auxiliary array for merging
Stable Yes Take left element on ties
In place No Needs extra array
Recurrence 2T(n/2) + n Master theorem Case 2

Example 2.2: Merge sort by hand

Problem: Sort 38, 27, 43, 3, 9, 82, 10 using merge sort.

Solution:

  1. Split: [38, 27, 43, 3] and [9, 82, 10]; again: [38, 27], [43, 3], [9, 82], [10].
  2. Sort the pairs: [27, 38], [3, 43], [9, 82], [10].
  3. Merge: [27, 38] and [3, 43] give [3, 27, 38, 43]; [9, 82] and [10] give [9, 10, 82].
  4. Final merge of [3, 27, 38, 43] and [9, 10, 82] gives [3, 9, 10, 27, 38, 43, 82].

Result: Sorted array 3, 9, 10, 27, 38, 43, 82.

count = 0
def msort(a):
    global count
    if len(a) <= 1:
        return a
    m = len(a) // 2
    L, R = msort(a[:m]), msort(a[m:])
    out, i, j = [], 0, 0
    while i < len(L) and j < len(R):
        count += 1
        if L[i] <= R[j]:
            out.append(L[i]); i += 1
        else:
            out.append(R[j]); j += 1
    return out + L[i:] + R[j:]
print(msort([38, 27, 43, 3, 9, 82, 10]))
print("comparisons:", count)

Output

[3, 9, 10, 27, 38, 43, 82]
comparisons: 13

Common mistakes

  • Saying merge sort is in place; it needs an auxiliary array.
  • Copying the right element first on ties, which makes the sort unstable.
  • Forgetting to copy the remaining elements after one half is exhausted.
  • Claiming the best case is Θ(n); with the standard algorithm it is still Θ(n log n).

Key takeaways

  • Merge sort: divide in halves, sort recursively, merge.
  • T(n) = 2T(n/2) + n gives Θ(n log n) in all cases.
  • It is stable but needs Θ(n) extra space.
  • Merging two sorted lists of total size n takes Θ(n).

Practice questions

  1. What is the recurrence for merge sort?
  2. Give the space complexity of merge sort.
  3. Why is merge sort stable?

Answers to the practice questions

  1. T(n) = 2T(n/2) + n.
  2. Θ(n) for the auxiliary array.
  3. On equal keys the element from the left half is copied first.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II