ABSTRACT

Merge Sort is a comparison-based sorting algorithm that uses the Divide and Conquer strategy to achieve a guaranteed time complexity. It relies on the fact that merging two sorted lists is significantly more efficient () than sorting an unsorted one from scratch.


The Strategy

  1. Divide: Split the unsorted list into two sub-lists of roughly size.
  2. Recursively Sort: Call MergeSort on both sub-lists until base cases are reached.
  3. Conquer (Merge): Combine the two sorted sub-lists into one sorted result using the RMerge helper.


Formal Proof of Correctness

Part 1: The Merge Helper (RMerge)

We prove the helper function using Regular Induction because the total number of elements () decreases by exactly 1 in each recursive call.

  • Base Case: If both lists are empty (), it returns an empty list, which is sorted.
  • Inductive Hypothesis: Assume RMerge correctly merges any two sorted lists with combined size .
  • Inductive Step: For a combined size , we compare the heads (). The smaller element is prepended to the result of RMerge called on the remaining elements. By the hypothesis, the sub-call is correct; thus, the final list is sorted.

Part 2: The Main Algorithm

We prove MergeSort using Strong Induction because each subsequent call halves the input size ().

  • Base Case:
    • : Returns an empty list (trivially true).
    • : Returns , a trivially sorted list containing all elements.
  • Inductive Hypothesis: Assume MergeSort correctly sorts all lists with elements for any , where .
  • Inductive Step:
    1. Divide the list of size into two halves of size and .
    2. By the Strong Inductive Hypothesis, since both halves have size , the recursive calls and return correctly sorted lists.
    3. By the correctness of RMerge, results in a sorted list of all elements.

Time Analysis

1. Recurrence Extraction

Let be the runtime of MergeSort on a list of size .

Since , the expression is:

2. Method A: Master Theorem

Comparing to :

  • (number of recursive calls)
  • (factor by which size is reduced)
  • (exponent of non-recursive work )

Comparison: and .

Since , we use Case 2:

3. Method B: Unraveling (Iteration)

We substitute the recurrence into itself to find the pattern:

To reach the base case , we set :