Professional Documents
Culture Documents
Lecture 06 Daa 2
Lecture 06 Daa 2
QUICK SORT
SUBJECT-CODE : KCS-503
Dr. Ragini Karwayun
QUICKSORT
• Quicksort advantages:
• Sorts in place
• Sorts O(n lg n) in the average case
• Very efficient in practice
• Quicksort disadvantages :
• Sorts O(n2) in the worst case (Input array is sorted in any order)
• Not stable
• Does not preserve the relative order of elements with equal keys
• Sorting algorithm is stable if 2 records with same key stay in original order
• But in practice, it’s quick
• And the worst case doesn’t happen often.
IPEC KCS-503. Design and Analysis of Algorithms Dr. Ragini Karwayun
1
9/29/22
QUICK SORT
Another divide-and-conquer algorithm:
• Combine: Unlike merge sort, no work needed since the subarrays are sorted in place
already.
QUICKSORT ALGORITHM
Quicksort(A, p, r)
{
If (p < r)
{
q = partition(A, p, r)
Quicksort(A, p, q-1)
Quicksort(A, q+1 , r)
}
}
2
9/29/22
PARTITION ALGORITHM
Partition(A, p, r)
{
x = A[r] // x is pivot
i=p–1
For j = p to r – 1
{
If a[j] <= x
{
i= i + 1
Exchange A[i] « A[j]
}
}
Exchange A[i+1] « A[r]
return i+1
BASIC IDEA
• PARTITION always selects an element q = A[r] as a pivot element around which to
partition the subarray A[ p . . r ].
• As the procedure runs, the array is partitioned into four (possibly empty) regions.
• All entries in A[p..i] are ≤ (pivot)
• All entries in A[i+1….j-1] are > (pivot)
• A[r] = pivot
3
9/29/22
EXAMPLE
i p,j r x=5 If A[j] ≤ x
8 1 6 4 0 3 9 5 𝑛𝑜
i p j
8 1 6 4 0 3 9 5 𝑦𝑒𝑠 𝑖 = 𝑖 + 1 & 𝑠𝑤𝑎𝑝 𝐴 𝑖 ↔ 𝐴[𝑗]
i,p j
1 8 6 4 0 3 9 5 𝑛𝑜
p i j
1 8 6 4 0 3 9 5 𝑦𝑒𝑠 𝑖 = 𝑖 + 1 & 𝑠𝑤𝑎𝑝 𝐴 𝑖 ↔ 𝐴[𝑗]
p i j
1 4 6 8 0 3 9 5 𝑦𝑒𝑠 𝑖 = 𝑖 + 1 & 𝑠𝑤𝑎𝑝 𝐴 𝑖 ↔ 𝐴[𝑗]
p i j
1 4 0 8 6 3 9 5 𝑦𝑒𝑠 𝑖 = 𝑖 + 1 & 𝑠𝑤𝑎𝑝 𝐴 𝑖 ↔ 𝐴[𝑗]
p i j
1 4 0 3 6 8 9 5 𝑛𝑜
1 4 0 3 5 8 9 6 𝑒𝑥𝑐ℎ𝑎𝑛𝑔𝑒 𝐴 𝑖 + 1 ↔ 𝐴[𝑟]
i+1 r
IPEC KCS-503. Design and Analysis of Algorithms Dr. Ragini Karwayun
EXAMPLE CONT..
• PARTITION will return q = 5
• After this we will call QUICKSORT(A,1,4) QUICKSORT(A,6,8)
1 4 0 3 8 9 6
1 4 0 3 6 9 8
1 0 4 3 9 8
1 0 3 4 .
.
1 0 4 .
.
.
Recursively until we get the sorted array.
4
9/29/22
PERFORMANCE OF QUICKSORT
• The running time of quicksort depends on the partitioning of the subarrays.
• If the subarrays are balanced, then quicksort can run as fast as merge sort.
• If they are unbalanced, then quicksort can run as slowly as insertion sort.
• Assuming that all inputs are distinct elements, the running time of the algorithm
depends on the distribution of splits.
ANALYZING QUICKSORT
10
5
9/29/22
ANALYZING QUICKSORT
• Worst case for the algorithm?
• Partition is always unbalanced à Sorted array (either way) , all elements same
• What will be the best case for the algorithm?
• Partition is perfectly balanced. à pivot nearly divide the array into two parts
• Which is more likely?
11
BEST CASE
• Ideal situation à partition splits the array evenly in every recursion.
T (n) = 2T (n / 2) + Q(n)
12
6
9/29/22
WORST CASE
• When the array is sorted à One side of the partition has n-1 element and the other
with 0 elements.
= Q( n 2 )
13
WORST CASE
Worst case occurs in quicksort when the input array is sorted. Whereas an array sorted in increasing order
will be the best case for insertion sort.
14
7
9/29/22
AVERAGE CASE
• Assuming random input, average-case running time is much closer to O(n lg n)
than O(n2)
• First, a more intuitive explanation/example:
• Suppose that partition() always produces a 9-to-1 split. This looks quite
unbalanced!
• The recurrence is thus:
T(n) <=T(9n/10) + T(n/10) + n
• how deep will the recursion go?
15
AVERAGE CASE
Suppose the split is 1/10 : 9/10
T (n) = T (n /10) + T (9n /10) + Q(n) = Q(n log n)!
16
8
9/29/22
18
EXERCISES
1. Illustrate the operation of PARTITION on the array A = 13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6,
21.
2. What value of q does PARTITION return when all elements in the array a[p . . r] have
the same value?
3. Use the substitution method to prove that the recurrence T (n) = T (n −1)+(n) has
the solution T (n) = (n2).
4. What is the running time of QUICKSORT when all elements of array A have the same
value?
5. Show that the running time of QUICKSORT is (n2) when the array A contains distinct
elements and is sorted in decreasing order.
19
9
9/29/22
RANDOMIZED QUICKSORT
• Assume all elements are distinct
• Partition around a random element
• Consequently, all splits (1:n-1, 2:n-2, ..., n-1:1) are equally likely with probability 1/n
• Randomization is a general tool to improve algorithms with bad worst-case but good
average-case complexity
20
RANDOMIZED QUICKSORT
Randomized-Partition(A,p,r)
1 i = Random(p,r)
2 exchange A[r] «A[i]
3 return Partition(A,p,r)
Randomized-Quicksort(A,p,r)
1 if p<r then
2 q = Randomized-Partition(A,p,r)
3 Randomized-Quicksort(A,p,q)
4 Randomized-Quicksort(A,q+1,r)
21
10