0% found this document useful (0 votes)
7 views

DAA_LECT_8

he

Uploaded by

Varun Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DAA_LECT_8

he

Uploaded by

Varun Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

LECTURE-8

Divide & Conquer Techniques

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

Recursive call Recursive call

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

Similarly This Process Continue


Quicksort Analysis
• Assume that keys are random, uniformly
distributed.
• We consider the Best , Worst and Average
cases of Quick sort.
Worst Case Partitioning

When Array is sorted or Reverse Sorted then it


will be the worst case of Quick sort.
Worst Case Partitioning
.
Worst Case Partitioning
• Worst-case partitioning
– One region has one element and the other has n – 1 elements

– 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

T(n) = 0(n log n) (by Recursion tree Method)


RANDOMIZED
Randomized Quicksort
QUICK SORT
Randomized Algorithms
• No input can elicit worst case behavior
– Worst case occurs only if we get “unlucky”
numbers from the random number generator
• Worst case becomes less likely
– Randomization can NOT eliminate the worst-case
but it can make it less likely!

26
Randomizing Quicksort

• Randomly permute the elements of the input array before


sorting
• OR ... modify the PARTITION procedure
– At each step of the algorithm we exchange element
A[r] with an element chosen at random from A[p…r]
– The pivot element x = A[r] is equally likely to be any
one of the r – p + 1 elements of the subarray
Randomized PARTITION
Alg.: RANDOMIZED-PARTITION(A, p, r)
i ← RANDOM(p, r)
exchange A[r] ↔ A[i]
return PARTITION(A, p, r)

Almost the same as Partition, but now the


pivot element is not the rightmost element, but
rather an element from A[p..r] that is chosen
uniformly at random.
Randomized Quicksort
Alg. : RANDOMIZED-QUICKSORT(A, p, r)
if p < r
then q ← RANDOMIZED-PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, q-1)
RANDOMIZED-QUICKSORT(A, q + 1, r)
No worst case in Randomized Quick Sort Always Average Case
in Randomized Quick Sort. So always running time will be θ (n
log n)
• 9-to-1 proportional split
T(n) = T(9n/10) + T(n/10) + n

T(n) = 0(n log n) (by Recursion tree Method)

You might also like