Time and Space Complexity of Merge Sort

Merge Sort is a popular sorting algorithm known for its efficiency and stability. In this article, we’ll analyze the time and space complexity of Merge Sort, understand why it’s so efficient, and compare it with other sorting algorithms.

Time Complexity of Merge Sort

Merge Sort has a time complexity of O(n log n) in all cases: best, average, and worst. This makes it highly efficient compared to algorithms like Bubble Sort (O(n²)) for large datasets. Let’s see why:

How Merge Sort Works (Brief Recap)

  1. Divide: Split the array into two halves.
  2. Conquer: Recursively sort each half.
  3. Combine: Merge the sorted halves back together.

The magic lies in the “divide and conquer” strategy. Let’s visualize this with an example of sorting an array of 8 elements:

  • Divide: Split the array into halves until you get single elements.
    [5, 3, 8, 4, 2, 7, 1, 6]
    → [5, 3, 8, 4] and [2, 7, 1, 6]
    → [5, 3], [8, 4], [2, 7], [1, 6]
    → [5], [3], [8], [4], [2], [7], [1], [6]
    
  • Merge: Combine pairs of single-element arrays into sorted subarrays.
    [3, 5], [4, 8], [2, 7], [1, 6]
    → [3, 4, 5, 8], [1, 2, 6, 7]
    → [1, 2, 3, 4, 5, 6, 7, 8]
    

Refer to these articles for better understanding on how merge sort algorithm works:

Why O(n log n)?

  • Division (log n levels): Each split divides the array into halves. For an array of size n, this splitting happens log₂n times (the number of times you can divide n by 2 until you reach arrays of size 1). For 8 elements, log₂8 = 3 splits.
  • Merging (O(n) per level): At each level of merging, every element is compared and placed in order. This takes O(n) time per level.

Total time = O(n) per level × log n levels = O(n log n).

No matter how the input is arranged, Merge Sort always divides the array into halves and merges them methodically. This consistency guarantees O(n log n) performance, unlike algorithms like Quick Sort, which can degrade to O(n²) in the worst case.

Space Complexity of Merge Sort

Merge Sort has a space complexity of O(n). This means it requires extra memory proportional to the input size. Here’s why:

How Merge Sort Uses Memory

  1. Temporary Arrays: During the merge phase, the algorithm creates temporary arrays to combine sorted subarrays. For example, merging two subarrays of size 4 requires a temporary array of size 8.
  2. Recursion Overhead: The recursive calls use a small amount of memory for the call stack (O(log n)), but this is negligible compared to the temporary arrays.

Why O(n) and Not O(n log n)?

You might wonder: If merging happens at every level, why isn’t the space O(n log n)? The answer is that temporary arrays are reused and discarded after merging. At any point, the maximum extra memory used is O(n) (e.g., storing a merged array of size n).

Comparison with Other Sorting Algorithms

Algorithm Time Complexity (Best) Time Complexity (Average) Time Complexity (Worst) Space Complexity
Merge Sort O(n log n) O(n log n) O(n log n) O(n)
Quick Sort O(n log n) O(n log n) O(n²) O(log n)
Bubble Sort O(n) O(n²) O(n²) O(1)
Insertion Sort O(n) O(n²) O(n²) O(1)
Selection Sort O(n²) O(n²) O(n²) O(1)

Key Observations

  • Merge Sort is consistent and performs well in all cases (best, average, and worst).
  • Unlike Quick Sort, Merge Sort does not degrade to O(n²) in the worst case.
  • Merge Sort requires additional space for merging, which makes it less space-efficient than in-place algorithms like Quick Sort or Bubble Sort.

In the next article, we’ll explore the advantages and disadvantages of Merge Sort in more detail and compare it with other sorting algorithms.