You are on page 1of 23

Quicksort

A Divide and Conquer Paradigm

Instructor: Ashok Singh Sairam


Lecture Plan
• Understand the Quick sort approach
§ Examine the divide and conquer approach
§ Examples
• Proof of correctness
• Running Time

MA512: Data Structures and Algorithms


2
Quicksort: Overview

MA512: Data Structures and Algorithms


3
Divide and conquer: Divide
• Sort an array A[p…q]
• Divide: Partition array into two subarrays around
a pivot x such that
§ elements in lower subarray ≤ x
§ x ≤ elements in upper subarray.
§ Need to find index of x to partition the array

MA512: Data Structures and Algorithms


4
Divide and conquer: Conquer
• 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
• The key step is partitioning

MA512: Data Structures and Algorithms


5
Partitioning subroutine
Algorithm PARTITION(A, p, r)
x ← A[r] ⊳ pivot
i ← p -1
for j ← p to r-1 do
if A[ j] ≤ x then
i←i+1
exchange A[i] ↔ A[ j]
exchange A[i+1] ↔ A[r]
return i+1

MA512: Data Structures and Algorithms


6
Partition in Action

Courtesy: Intro to Algo: CLSR


MA512: Data Structures and Algorithms
7
Quicksort Algorithm

Algorithm QUICKSORT(A, p, r)
if p < r then
q  PARTITION(A, p, r)
QUICKSORT (A, p, q-1)
QUICKSORT (A, q+1, r)

MA512: Data Structures and Algorithms


8
Proof of Correctness (1)

MA512: Data Structures and Algorithms


9
Proof of Correctness (2)
• Initialization
• i=p-1, j=p
• No elements between p and i
§ First condition is true
• No elements between i+1 and j-1
§ Second condition is true
• The assignment (x ← A[r] ) in line 1 satisfies the
third condition

MA512: Data Structures and Algorithms


10
Proof of Correctness(3)

MA512: Data Structures and Algorithms


11
Proof of Correctness (4)
• Termination: occur when j=r
• Every entry in the array is in one of the three sets
described by the invariant
• The final two lines of the algorithm moves the
pivot to its correct place and returns the index of
the pivot
exchange A[i+1] ↔ A[r]
return i+1

MA512: Data Structures and Algorithms


12
Running Time: Worst Case
• Running time of quicksort will depend on whether
the partitioning is balanced or skewed
• Worst case partitioning
§ When one subarray has n-1 elements and the other
subarray is NULL n n
1 n-1 n
1 n-2 n-1
n 1 n-3 n-2

1
2 3
1 1 2

(n2)
MA512: Data Structures and Algorithms
13
Running Time: Best Case

MA512: Data Structures and Algorithms


14
Running Time: Balanced Partitioning
• Average case running time is much closer to the
best case (than to the worst case)
• Suppose partitioning algorithm always produces a
9-to-1 proportional split
• Recurrence

MA512: Data Structures and Algorithms


15
Recurrence Tree: Balanced Partitioning

MA512: Data Structures and Algorithms


16
Recurrence Tree: Balanced Partitioning

MA512: Data Structures and Algorithms


17
Recurrence Tree: Balanced Partitioning

MA512: Data Structures and Algorithms


18
Recurrence Tree: Balanced Partitioning

Conclusion: A 9:1 split is asymptotically same as a 1:1 split


MA512: Data Structures and Algorithms
19
How partition affect performance

MA512: Data Structures and Algorithms


20
Running Time: Average case intuition
• All permutations of the input numbers are equally likely
• On a random input array, we will have a mix of well
balanced and unbalanced splits
• Good and bad splits are randomly distributed across
throughout the tree
• For the sake of intuition, suppose the good and bad splits
occur at alternate levels of the tree

MA512: Data Structures and Algorithms


21
Running Time: Average case

MA512: Data Structures and Algorithms


22
Running Time: Average case

MA512: Data Structures and Algorithms


23

You might also like