DAA_LECT_8
DAA_LECT_8
QUICK SORT
Faculty: Devesh Garg
Department of Computer Science &
Engineering
Quicksort Algorithm
Given an array of n elements (e.g., integers):
• If array only contains one element, return
• Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Quicksort two sub-arrays
– Return results
Quicksort
• Sort an array A[p…r]
• Divide
• Partition (separate) the array A[p..r] into two
(possibly empty) subarrays A[p..q–1] and
A[q+1..r].
– Each element in A[p..q–1] < A[q].
– A[q] < each element in A[q+1..r].
– Index q is computed as part of the partitioning
procedure.
≤
A[p…q-1] A[q+1…r]
Quicksort
A[p…q-1] A[q+1…r]
≤
• Conquer
– Recursively sort A[p..q-1] 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
Quicksort example
81 43 31 57 75
13 92 65 26 0
Select pivot
81 43 31 57 75
13 65 26 0
92
partition
13 31 57 81
26 0 65 92 75
43
0 13 26 31 43 57 75 81 92
Merge
0 13 26 31 43 57 65 75 81 92
Partitioning
• Select the last element A[r] in the subarray A[p..r] as
the pivot – the element around which to partition.
• As the procedure executes, the array is partitioned into
four (possibly empty) regions.
1. A[p..i ] — All entries in this region are ≤ pivot.
2. A[i+1..j – 1] — All entries in this region are > pivot.
3. A[r] = pivot.
4. A[j..r – 1] — Not known how they compare to pivot.
• The above hold before each iteration of the for loop,
and constitute a loop invariant. (4 is not part of the loopi.)
Quicksort(A, p, r)
if p < r then Partition(A, p, r)
q := Partition(A, p, r); x = A[r];
Quicksort(A, p, q – 1); i = p – 1;
Quicksort(A, q + 1, r) for j := p to r – 1 do
if A[j] x then
A[p..r] i := i + 1;
A[i] A[j]
5 A[i + 1] A[r];
return i + 1
A[p..q – 1] A[q+1..r]
Partition 5
5 5
Partition(A, p, r)
x = A[r];
i = p – 1;
for j := p to r – 1 do
if A[j] x then
i := i + 1;
A[i] A[j]
A[i + 1] A[r];
return i + 1
Example
p r
initially: 2 5 8 3 9 4 1 7 10 6 note: pivot (x) = 6
i j
next iteration: 2 5 8 3 9 4 1 7 10 6
i j
next iteration: 2 5 8 3 9 4 1 7 10 6
i j
next iteration: 2 5 8 3 9 4 1 7 10 6
i j
next iteration: 2 5 3 8 9 4 1 7 10 6
i j
Example (Continued)
next iteration: 2 5 3 8 9 4 1 7 10 6
i j
next iteration: 2 5 3 8 9 4 1 7 10 6 Partition(A, p, r)
i j x = A[r];
next iteration: 2 5 3 4 9 8 1 7 10 6 i = p – 1;
i j for j := p to r – 1 do
next iteration: 2 5 3 4 1 8 9 7 10 6 if A[j] x then
i j i := i + 1;
next iteration: 2 5 3 4 1 8 9 7 10 6 A[i] A[j]
i j A[i + 1] A[r];
next iteration: 2 5 3 4 1 8 9 7 10 6 return i + 1
i j
after final swap: 2 5 3 4 1 6 9 7 10 8
i j
Example
p r
Now for Left Side portion : 2 5 3 4 1 note: pivot (x) = 1
i j
next iteration: 2 5 3 4 1
i j
next iteration: 2 5 3 4 1
i j
next iteration: 2 5 3 4 1
i j
next iteration: 2 5 3 4 1
i j
– Maximally unbalanced
n n
• Recurrence: q=1
1 n-1 n
T(n) = T(0) + T(n – 1) + n, 1 n-2 n-1
T(1) = (1)
n 1 n-3 n-2
1
T(n) = T(n – 1) + n 2 3
1 1 2
n
n + k − 1 = ( n ) + ( n 2 ) = ( n 2 ) (n 2)
=
k =1
When does the worst case happen?
Best Case Partitioning
• Best-case partitioning
– Partitioning produces two regions of size n/2
Best Case Partitioning
• Best-case partitioning
– Partitioning produces two regions of size n/2
• Recurrence: q=n/2
T(n) = 2T(n/2) + (n)
T(n) = (nlgn) (Master theorem)
Case Between Worst and Best
• 9-to-1 proportional split
T(n) = T(9n/10) + T(n/10) + n
26
Randomizing Quicksort