You are on page 1of 3

AOAD Assignment

SE Computers

AOAD Report
Topic: Quick Sort(Average Case)

Quicksort is developed by Tony Hoare that, on average, makes O(nlogn) (big O notation)
comparisons to sort n items. Quicksort is a divide and conquer algorithm. Quicksort first divides a
large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then
recursively sort the sub-lists.
The steps are:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements with values less than the pivot come before the pivot,
while all elements with values greater than the pivot come after it (equal values can go either
way). After this partitioning, the pivot is in its final position. This is called
the partition operation.
3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
Average Case:QuickSort
T(n) = T(1)+T(n-1)+n+1
= T(2)+T(n-2)+n+1
= T(3)+T(n-3)+n+1
:
:
=T(n-2)+T(2)+n+1
=T(n-1)+T(1)+n+1
T(n)= [2[T(1)+T(2)+T(3)+...+T(n-2)+T(n-1)]+(n-1)(n+1)]/n
nT(n)=2[T(1)+T(2)+T(3)++T(n-2)+T(n-1)]+(n-1)(n+1)
-------(1)
(n-1)T(n-1)=2[T(1)+T(2)+T(3)+..+T(n-3)+T(n-2)]+(n-2)n
-------(2)
Subtracting (2) from (1):

nT(n) (n-1)T(n-1) = 2T(n-1)+(n-1)(n+1)-n(n-2)


2

= 2T(n-1) + n - 1 - n +2n
nT(n) (n-1)T(n-1) = 2T(n-1) + 2n 1
nT(n) = (n+1)T(n-1) + 2n 1
Divide by n,
T(n) = [(n+1)/n]T(n-1) + 2
Divide by (n+1),
T(n)/(n+1) = T(n-1)/n + 2/n+1
T(n-1)/n=T(n-2)/n-1 + 2/n
T(n-2)/n-1=T(n-3)/n-2 + 2/n-1
T(n-k)/ n-(k-1) = T(n-(k+1))/n-k +

-------(3)
-------(4)
-------(5)
2/n-(k-1)

if k=n-1
T(1)/2 = T(0)/1 + 2/2

-------(6)

where T(0)=0

Adding (3),(4),(5),(6),
T(n)/n+1 + T(n-1)/n + T(n-2)/n-1 + + T(1)/2 = T(n-1)/n+ 2/n+1 +
T(n-2)/n-1 + 2/n + T(n-3)/n-2 + 2/n-1 + . + 2/2
T(n)/n+1 = 2/n+1 + 2/n + 2/n-1 + + 2/2= 2[1/2 + 1/3 + . + 1/n-1 + 1/n + 1/n+1]
=2[logen]
T(n) =2(n+1)[logen]
=2(n+1)[log2n]/[log2e]
T(n) =O(nlog2n)

You might also like