KEMBAR78
09 QUICK SORT Design and Analysis of algorithms | PPTX
Design & Analysis of Algorithms
Session 9
Quick Sort
Design and Analysis of Algorithms
•Session – 8
Introduction to Divide and Conquer
Merge Sort
• QuickSort is a Divide and Conquer algorithm.
• It picks an element as pivot and partitions the given array around the picked pivot.
• There are many different versions of quickSort that pick pivot in different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
QUICKSORT
QUICKSORT
• Sort an array A[p…r]
• Divide
– Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that
each element of A[p..q] is smaller than or equal to each element in
A[q+1..r]
– Need to find index q to partition the array
≤
A[p…q] A[q+1…r]
QUICKSORT
• Conquer
– Recursively sort A[p..q] and A[q+1..r] using Quicksort
• Combine
– Trivial: the arrays are sorted in place
– No additional work is required to combine them
– The entire array is now sorted
A[p…q]
A[q+1…r]
≤
QUICK SORT
• Divide:
• Pick any element as the pivot, e.g, the first element
• Partition the remaining elements into
FirstPart, which contains all elements < pivot
SecondPart, which contains all elements > pivot
• Recursively sort FirstPart and SecondPart.
• Combine: no work is necessary since sorting is done in place.
PIVOT DIVIDES A INTO TWO SUBLISTS X AND Y.
4 2 7 8 1 9 3 6 5
4
pivo
t
4 2 7 8 1 9 3 6 5
x y
Keep going from left side as long as a[ i ]<pivot and from the right
side as long as a[ j ]>pivot
85 24 63 95 17 31 45 98
i j
85 24 63 95 17 31 45 98
85 24 63 95 17 31 45 98
85 24 63 95 17 31 45 98
i
i
i
j
j
j
Example :
Process:
pivot
If i<j interchange ith
and j th
elements and
then Continue the process.
85 24 63 45 17 31 95 98
i j
85 24 63 45 17 31 95 98
i j
85 24 63 45 17 31 95 98
i
85 24 63 45 17 31 95 98
i
j
85 24 63 45 17 31 95 98
j
If i ≥j interchange jth
and pivot elements
and then divide the list into two sublists.
i
31 24 63 45 17 85 95 98
Two sublists:
31 24 63 45 17 95 98
Recursively sort
FirstPart and SecondPart
QickSort( low, j-1 ) QickSort( j+1,high )
j
85
Quick Sort Algorithm :
Algorithm QuickSort(low,high)
//Sorts the elements a[low],…..,a[high] which resides in the global array a[1:n] into //ascending
order;
// a[n+1] is considered to be defined and must ≥ all the elements in a[1:n].
{
if( low< high ) // if there are more than one element
{ // divide p into two subproblems.
j :=Partition(low,high);
// j is the position of the partitioning element.
QuickSort(low,j-1);
QuickSort(j+1,high);
//There is no need for combining solutions.
}
}
Algorithm Partition(l,h)
{
pivot:= a[l] ; i:=l; j:= h+1;
while( i < j ) do
{
i++;
while( a[ i ] < pivot ) do
i++;
j--;
while( a[ j ] > pivot ) do
j--;
if ( i < j ) then Interchange(i,j ); // interchange ith
and
} // jth
elements.
Interchange(l, j ); return j; // interchange pivot and jth
element.
}
Algorithm interchange (x,y )
{
temp=a[x];
a[x]=a[y];
a[y]=temp;
}
TIME COMPLEXITIES OF QUICK SORT
A BEST/GOOD CASE
• It occurs only if each partition divides the list into two equal
size sublists.
• T(n)=2T(n/2)+cn =O(nlog n)
O(n logn)
Best/good Case
• Total time: O(nlogn)
n
n/2 n/2
n/4 n/4 n/4 n/4
2 2 2
BEST CASE TIME COMPLEXITY
• To do average case analysis, we need to consider all possible permutation of array and calculate
time taken by every permutation which doesn’t look easy.
• We can get an idea of average case by considering the case when partition puts O(n/9)
elements in one set and O(9n/10) elements in other set.
• Following is recurrence for this case.
• T(n) = T(n/9) + T(9n/10) + (n)
• T(n)=O(nlogn)
AVERAGE CASE COMPLEXITY
TIME COMPLEXITY ANALYSIS
A WORST/BAD CASE
T(N)=T(N-1)+CN=O(N2
)
8
7
6
5
4
3
2
1
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8
3 4 5 6 7 8
4 5 6 7 8
5 6 7 8
6 7 8
7 8
8
O(n2
)
9
9
9
9
9
9
9
9
9
9
cn
c(n-1)
3c
2c
n
n-1
n-2
3
2
c(n-2)
Happens only if
• input is sortd
• input is reversely sorted
WORST/BAD CASE
Total time:
O(n2
)
1
1c
Worst-Case Analysis:
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, using above equation repeatedly. 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
T(N) = T(1) + c((N+1)(N/2)-1) =O(N^2) as claimed
earlier.
1. Sort the following elements using Quick sort technique:
54, 26, 93, 17, 77, 31, 44, 55, 20
2. Sort the following elements using Quick sort technique:
10, 80, 30, 90, 40, 50, 70, 60
EXERCISE PROBLEMS
22
SAMPLE QUESTIONS
• Differentiate about merge sort and quick sort
• Sort the following elements 45,90,5,50,25,17,17,100
• What is pivot element
• Explain in detail about analysis of quick sort

09 QUICK SORT Design and Analysis of algorithms

  • 1.
    Design & Analysisof Algorithms Session 9 Quick Sort Design and Analysis of Algorithms •Session – 8 Introduction to Divide and Conquer Merge Sort
  • 2.
    • QuickSort isa Divide and Conquer algorithm. • It picks an element as pivot and partitions the given array around the picked pivot. • There are many different versions of quickSort that pick pivot in different ways. 1. Always pick first element as pivot. 2. Always pick last element as pivot (implemented below) 3. Pick a random element as pivot. 4. Pick median as pivot. QUICKSORT
  • 3.
    QUICKSORT • Sort anarray A[p…r] • Divide – Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r] – Need to find index q to partition the array ≤ A[p…q] A[q+1…r]
  • 4.
    QUICKSORT • Conquer – Recursivelysort A[p..q] and A[q+1..r] using Quicksort • Combine – Trivial: the arrays are sorted in place – No additional work is required to combine them – The entire array is now sorted A[p…q] A[q+1…r] ≤
  • 5.
    QUICK SORT • Divide: •Pick any element as the pivot, e.g, the first element • Partition the remaining elements into FirstPart, which contains all elements < pivot SecondPart, which contains all elements > pivot • Recursively sort FirstPart and SecondPart. • Combine: no work is necessary since sorting is done in place.
  • 6.
    PIVOT DIVIDES AINTO TWO SUBLISTS X AND Y. 4 2 7 8 1 9 3 6 5 4 pivo t 4 2 7 8 1 9 3 6 5 x y
  • 7.
    Keep going fromleft side as long as a[ i ]<pivot and from the right side as long as a[ j ]>pivot 85 24 63 95 17 31 45 98 i j 85 24 63 95 17 31 45 98 85 24 63 95 17 31 45 98 85 24 63 95 17 31 45 98 i i i j j j Example : Process: pivot
  • 8.
    If i<j interchangeith and j th elements and then Continue the process. 85 24 63 45 17 31 95 98 i j 85 24 63 45 17 31 95 98 i j 85 24 63 45 17 31 95 98 i
  • 9.
    85 24 6345 17 31 95 98 i j 85 24 63 45 17 31 95 98 j If i ≥j interchange jth and pivot elements and then divide the list into two sublists. i
  • 10.
    31 24 6345 17 85 95 98 Two sublists: 31 24 63 45 17 95 98 Recursively sort FirstPart and SecondPart QickSort( low, j-1 ) QickSort( j+1,high ) j 85
  • 11.
    Quick Sort Algorithm: Algorithm QuickSort(low,high) //Sorts the elements a[low],…..,a[high] which resides in the global array a[1:n] into //ascending order; // a[n+1] is considered to be defined and must ≥ all the elements in a[1:n]. { if( low< high ) // if there are more than one element { // divide p into two subproblems. j :=Partition(low,high); // j is the position of the partitioning element. QuickSort(low,j-1); QuickSort(j+1,high); //There is no need for combining solutions. } }
  • 12.
    Algorithm Partition(l,h) { pivot:= a[l]; i:=l; j:= h+1; while( i < j ) do { i++; while( a[ i ] < pivot ) do i++; j--; while( a[ j ] > pivot ) do j--; if ( i < j ) then Interchange(i,j ); // interchange ith and } // jth elements. Interchange(l, j ); return j; // interchange pivot and jth element. }
  • 13.
    Algorithm interchange (x,y) { temp=a[x]; a[x]=a[y]; a[y]=temp; }
  • 14.
    TIME COMPLEXITIES OFQUICK SORT A BEST/GOOD CASE • It occurs only if each partition divides the list into two equal size sublists. • T(n)=2T(n/2)+cn =O(nlog n) O(n logn)
  • 15.
    Best/good Case • Totaltime: O(nlogn) n n/2 n/2 n/4 n/4 n/4 n/4 2 2 2
  • 16.
    BEST CASE TIMECOMPLEXITY
  • 17.
    • To doaverage case analysis, we need to consider all possible permutation of array and calculate time taken by every permutation which doesn’t look easy. • We can get an idea of average case by considering the case when partition puts O(n/9) elements in one set and O(9n/10) elements in other set. • Following is recurrence for this case. • T(n) = T(n/9) + T(9n/10) + (n) • T(n)=O(nlogn) AVERAGE CASE COMPLEXITY
  • 18.
    TIME COMPLEXITY ANALYSIS AWORST/BAD CASE T(N)=T(N-1)+CN=O(N2 ) 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8 8 O(n2 ) 9 9 9 9 9 9 9 9 9 9
  • 19.
    cn c(n-1) 3c 2c n n-1 n-2 3 2 c(n-2) Happens only if •input is sortd • input is reversely sorted WORST/BAD CASE Total time: O(n2 ) 1 1c
  • 20.
    Worst-Case Analysis: The pivotis 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, using above equation repeatedly. 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 T(N) = T(1) + c((N+1)(N/2)-1) =O(N^2) as claimed earlier.
  • 21.
    1. Sort thefollowing elements using Quick sort technique: 54, 26, 93, 17, 77, 31, 44, 55, 20 2. Sort the following elements using Quick sort technique: 10, 80, 30, 90, 40, 50, 70, 60 EXERCISE PROBLEMS
  • 22.
    22 SAMPLE QUESTIONS • Differentiateabout merge sort and quick sort • Sort the following elements 45,90,5,50,25,17,17,100 • What is pivot element • Explain in detail about analysis of quick sort