Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Analysis of Recursive

Algorithms

Merge Sort Algorithm


MergeSort(A, i, j)
if j > i then
mid (i + j)/2
MergeSort(A, i, mid )
MergeSort(A, mid + 1, j )
Merge(A, i, mid, j )

Merge Algorithm

//LeftPos = start of left half;


//RightPos = start of right half
void Merge(int A[ ], int LeftPos, int RightPos, int RightEnd)
{
int LeftEnd = RightPos 1;
int TmpPos = 1
int NumElements = RightEnd LeftPos + 1;
int TempArray[NumElements];
while(leftPos <= LeftEnd && RightPos <= RightEnd)
if(A[LeftPos] <= A[RightPos])
TmpArray[TmpPos++] = A[LeftPos++];
else
TmpArray[TmpPos++] = A[RightPos++];
while(LeftPos <= LeftEnd) //Copy rest of first half
TmpArray[TmpPos++] = A[LeftPos++];
while(RightPos <= RightEnd) //Copy rest of second half
TmpArray[TmpPos++] = A[RightPos++];
for(int i = 1; i <= NumElements; i++) //Copy TmpArray back
A[LeftPos++] = TmpArray[i];
}

Another Way of Merge Sort


Analysis
Suppose n is a power of two, so that we
always split into even halves.
For n = 1 the time to merge sort is1 otherwise
The time to merge sort n numbers is equal to
the time to do two recursive merge sorts of
size n/2, plus the time to merge, which is
linear.
T(1) = 1
T(n) = 2T(n/2) + n
Solve this recurrence to find out running
time.

Solution by Telescoping Sum


T(n) = 2T(n/2) + n
Divide the recurrence relation through by n.
T(n) / n = T(n/2) / n/2 + 1
This equation is valid for any n that is a power of 2.
So
T(n/2) / n/2 = T(n/4) / n/4 + 1
T(n/4) / n/4 = T(n/8) / n/8 + 1
..
T(2) / 2 = T(1) / 1 + 1
Add both sides of all the above equations
T(n) / n + T(n/2) / n/2 + T(n/4) / n/4 + .+
T(2) / 2 = T(n/2) / 2 + 1 + T(n/4) / n/4 + 1 +
T(n/8) / n/8 + 1 +..+ T(1) / 1 + 1

Solution by Telescoping Sum


T(n) / n + T(n/2) / n/2 + T(n/4) / n/4 + .+ T(2) /
2 = T(n/2) / 2 + 1 + T(n/4) / n/4 + 1 + T(n/8) /
n/8 + 1 +..+ T(1) / 1 + 1
All the terms other than T(n) / n on right
hand side cancel the terms on left hand
side.
Since n is a power of 2, these equations add
up to lgn (Tree has lgn levels).
T(n) / n = T(1) / 1 + lgn
T(n) = n + nlgn
T(n) = O(nlgn)

Solution by Substitution
Method
T(n) = 2 T(n/2) + n
Substitute n/2 into the main equation
2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n
And T(n) = 4T (n/4) + 2n
Again by substituting n/4, we can see
4T(n/4) = 4(2T(n/8)) + (n/4) = 8T(n/8) + n
And T(n) = 8T(n/8) + 3n
Continuing in this manner, we obtain
T(n) = 2k T(n/2k) + k.n
Using k = lgn , 2k = n
T(n) = nT(1) + nlgn = nlgn + n
T(n) = O(nlgn)

You might also like