2. Points Covered:
Description of Quick sort
Algorithm of Quick sort
Example
Performance and recurrence relation
Best case partitioning and time complexity with derivation
Worst case partitioning and time complexity with derivation
Balanced partitioning and general equation for best case
Input for best and worst case
2
3. Description
This sorting algorithm uses Divide and Conquer approach , the three-step
divide-and-conquer process for sorting a array A[p….r]:
DIVIDE-Partition the array A[p…r] into two subarrays A[p…q-1] and
A[q+1..r] such that each element of left subarray is less than or equal to A[q]
and each element of right subarray is greater than or equal to A[q] , for this
partitioning we need to compute the index q.
Conquer-Sort the two subarrays by recursive calls to quick sort.
Combine-combine the two sorted subarrays
Quick sort is inplace and not stable sorting algo.
3
4. Procedure
Quicksort(A, p, r)
1. if (p < r)
2. q=partition(A, p, r)
3. Quicksort(A, p, q-1)
4. Quicksort(A, q+1, r)///initial call is Quicksort(A,0,n-1) where n is no. of elements in array
partition(A, p, r)
1. x=A[r]
2. i = p-1
3. for j =p to r-1
4. if (A[j]≤x)
5. i = i+1
6. exchange A[i] with A[j]
7. exchange A[i+1] with A[r]
8. return i+1
4
7. Performance and Recurrence Relation
1. The running time depends on whether the partition is balanced or unbalanced.
2. If the partition is balanced the algo runs asymptotically fast with time
complexity q(n logn).
3. If the partition is unbalanced , it can run asymptotically slow with time
complexity q(n2).
7
8. Best-case partitioning
This occurs when array is divided into two equal subarrays, each of size no more
than n/2, since one of size floor(n/2) and one of size ceil(n/2)-1.
The recurrence relation is :
T(n)=2T(n/2)+q(n)
Using master’s theorem we know time complexity will be q(n logn).
Proof: In general aT(n/b)+f(n) where a ≥ 1 and b>1 and f(n) is positive function
On comparing f(n) and nloga/logb
We get f(n) and nloga/logb asymptotically equal and by case 3 of master’s theorem
we know if f(n)=q(n(loga/logb) . (logn)k ) where k ≥ 0 then
T(n)=q(n(loga/logb).(logn)k+1 )
Hence time complexity in best case is q(n logn).
8
9. Worst Case partitioning
The worst case behaviour for quick sort occurs when the partitioning routine
produces one subproblem with n-1 elements and one with zero elements.
The recurrence relation is :
T(n)=T(n-1)+T(0)+ q(n)
T(n)=T(n-1)+ q(n)///T(1)=1 base case
Proof: By substitution method
T(n)=T(n-2)+n-1+n
T(n)=T(n-3)+n-2+n-1+n , if we do this n-1 times then
T(n)=T(n-(n-1))+(n-(n-2))+(n-(n-3))+………….n-1+n
T(n)=T(1)+2+3+4+……………………+n-1+n//////this is an ap series
9
10. Worst Case partitioning(continued)
T(n)=(n(n+1))/2 [sum of n natural numbers]
T(n) is approximately equal to q(n2).
Therefore the worst case running time of quicksort is q(n2).
Moreover the q(n2) running time occurs when the input array is already
completely sorted.
10
11. Balanced Partitioning
The average case running time of quicksort is much closer to the best case.
T(n) = n + T(an) + T((1-a)n)
where 0<a <1
Therefore the running time of quicksort in balanced partitioning will be q(nlogn).
11
12. Input for best case :- any unsorted array
example:
5 6 7 8 9 89 90 2
12
Input for worst case :- any sorted array
example:
5 5 5 5 5 5 5 5
5 6 7 8 9 10 11 20