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

Analysis of Recursive

Algorithms

Quick Sort Algorithm


QuickSort(A, left, right) {
if (right > left) then {
pivot = Partition(A, left, right);
QuickSort(A, left, pivot-1);
QuickSort(A, pivot+1, right);
}
}

Partition Algorithm

Partition(a[ ], left, right ) {


int i, j;
int Pivot = (left+right) / 2;
Swap(A[Pivot], A[right]);
i = left; j = right - 1;
Pivot = right;
while ( i < j ) {
while( a[ i ] <= a[Pivot]) i++;
while( a[ j ] >= a[Pivot]) j- -;
if ( i < j ) SWAP(a[i], a[ j ]);
}
Swap(A[ i ], A[Pivot])
return i
}

Analysis of Quick Sort


For analysis assume a random pivot (no
median-of-three partitioning) and no cutoff for
small files.
T(0) = T(1) = 1
T(n) = Running time of two recursive calls
+ linear time spent in partition and constant
time per pivot selection.
T(n) = T(i) + T(n i 1) + cn
Where i = |S1| is the number of elements in
S1

Worst-Case Analysis of Quick Sort


The pivot is the smallest element all the
time. Then
i = 0 and if we ignore T(0) = 1, which is
insignificant, the recurrence is
T(n) = T(n 1) + cn, n > 1
We telescope this equation thus
T(n 1) = T(n 2) + c(n 1)
T(n 2) = T(n 3) + c(n 2)

T(2) = T(1) + c(2)


Adding up all these equations yields
n

T (n) = T (1) + c i = O( n 2 )
i =2

Best-Case Analysis of Quick


Sort
In the best case, the pivot is in the middle.
To simplify math, we assume that
Two subfiles are each exactly half the size of the
original, although this gives
Slight overestimate, but this is acceptable because
we are only interested in a Bih-Oh answer.

T(n) = 2T(n/2) + cn.


The same recurrance yields by Merge Sort
therefore
T(n) = cnlgn + n = O(nlg n)

Master Theorem
The solution to the equation
T(n) = aT(n/b) + (nk), where a >= 1
and b >1 is

T(n)

O(nlogb a)
O(nklogn)
O(nk)

if a > bk
if a = bk
if a < bk

Solve the following recursion for the


Strassens Matrix Multiplication
Algorithm
T(n) = 8T(n/2) + O(n2)

Strassens Matrix Multiplication


Recurrence
Solve the following recurrence using
Master Theorem
T(n) = 8T(n/2) + O(n2)
a = 8, b = 2,
k=2
a > bk, 8 > 22

lg2 8

T(n) = O(n ) = O(n )

You might also like