Conquer: Recursively call MergeSort on the two subarrays until we are left with arrays of size 1, which are inherently sorted.
Combine: Merge the two sorted subarrays back into one single sorted array.
The comparisons happen only in the Combine (Merge) step.
Let's analyze the number of comparisons in the merge function. Suppose we are merging two sorted subarrays, one of size m1 and one of size m2. To determine the next element in the final merged array, we compare the top elements of the two subarrays. In the worst-case scenario, to merge a total of m1+m2 elements, we will make m1+m2−1 comparisons. This is because a comparison is made for each element placed into the final array, except for the very last element, which is placed without a comparison once the other subarray is exhausted.
In our case, we are always merging two sorted subarrays of equal size, say m. The total number of elements is 2m. The number of comparisons is therefore 2m−1.
2. Setting up the Recurrence Relation
Let C(n) be the total number of comparisons made by MergeSort on an array of size n.
Divide: Splitting the array into two halves requires 0 comparisons.
Conquer: We recursively sort two subarrays of size n/2. The number of comparisons for this step is C(n/2)+C(n/2)=2C(n/2).
Combine: We merge two sorted subarrays, each of size n/2. The total number of elements being merged is n/2+n/2=n. As established, this takes n−1 comparisons.
Combining these parts, we get the recurrence relation for the number of comparisons:
C(n)=2C(n/2)+n−1
The base case for the recursion is an array of size 1. An array of size 1 is already sorted and requires no comparisons.
C(1)=0
3. Proof by Induction
We will prove that C(n)=nlog2n−n+1 for n=2k using mathematical induction on k.
Let P(k) be the statement that the formula holds for n=2k:
C(2k)=2klog2(2k)−2.
Base Case (k=0)
For k=0, we have n=20=1.
From our recurrence's base case: C(1)=0.
From the formula: C(1)=1⋅log2(1)−1+1=1⋅0−1+1=0.
The formula holds for the base case k=0.
Inductive Hypothesis
Assume that P(k) is true for some integer k≥0. That is, we assume:
C(2k)=k⋅2k−2k+1
Inductive Step
We want to prove that P(k+1) is true. We need to show that the formula holds for n=2k+1.
The target formula for n=2k+1 is:
C(2k+1)=(k+1)⋅2
Let's start with the recurrence relation for C(2k+1):
C(2k+1)=2⋅C(2k+1/2)+2k+1−1C(2k+1)=2⋅C(2k)+
Now, we can apply our inductive hypothesis for C(2k):
C(2k+1)=2⋅(k⋅2k−2k+1)+2k+1−1
Let's simplify the expression:
C(2k+1)=2⋅k⋅2k−2⋅2k+2⋅1+2k+1−1C(2k+1)=k⋅2k+1C(2k+1)=k⋅2k+1+1
This expression needs to be manipulated to match our target formula. Let's look at the target again:
Target: (k+1)⋅2k+1−2k+1+1
Let's expand the target:
(k+1)⋅2k+1−
Our derived expression C(2k+1)=k⋅2k+1+1 exactly matches the target formula.
Thus, we have shown that if the formula holds for k, it also holds for k+1.
Conclusion
By the principle of mathematical induction, the formula C(n)=nlog2n−n+1 is true for all n that are a power of 2 (i.e., n=2k for k≥0).
Alternative Method: Unrolling the Recurrence
We can also solve the recurrence by repeatedly substituting it into itself.
Given n=2k:
C(n)=2C(n/2)+n−1C(n)=2[2C(n/4)+n/2−C(n)=4[2C(n/8)+n/4
A pattern emerges. After i steps of unrolling:
C(n)=2iC(n/2i)+i⋅n−∑j=0i−12j
The sum is a geometric series: ∑j=0i−12j=2−12i−1=2i−1.
So, C(n)=2iC(n/2i)+i⋅.
We continue unrolling until the base case is reached, which happens when n/2i=1. This means n=2i. Since we are given n=2k, this occurs when i=k. Note that k=log2n.
Substitute i=k into the equation:
C(n)=2kC(n/2k)+k⋅n−(2k−1)C(n)=2kC(1)+k⋅n−(2
Now substitute the known values C(1)=0, n=2k, and k=log2n:
C(n)=n⋅0+(log2n)⋅nC(n)=nlog2n−n+1