CSE-243: Algorithm Design and Analysis
Lecture 12-13: Quick Sort and Analysis
Instructor Teaching Assistants
Md. Sabir Hossain Rakibul Islam
Lecturer Id: 1604060
Dept. of CSE, CUET Email:
[email protected]Mobile: 01737143868/01882826575 Saiful Islam Rimon
Email:
[email protected] Id: 1604061
Email :
[email protected]Acknowledgment: Most of the content of this slide is adopted from the book: Fundamentals of Computer Algorithms by Ellis
Horowitz, Sanguthevar Rajasekaran, Sartaj Sahni, Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest, Clifford Stein.
Recap
• Review of Recursion
• Solving Techniques
– Substitution Method
– Recurrence Tree Method
– Master Method
2
Today’s Target
• Definitions
– Internal, External, In-Place Sort, Stability
• Classifications
– Comparisons Sorts
• Quicksort
• Heapsort
• Shellsort
• Merge sort
• Insertion sort
• Selection sort
• Bubble sort
– Non-Comparisons Sorts
• Counting sort (indexes using key values)
• Radix sort (examines individual bits of keys)
• Bucket sort (examines bits of keys)
3
Quick Sort Demo
4
Full example of quicksort (random set of numbers)
5
Example- partition
6
Quicksort
A[p…q] ≤ A[q+1…r]
• 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
7
Quicksort
A[p…q] ≤ A[q+1…r]
• 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
8
QUICKSORT
Alg.: QUICKSORT(A, p, r) Initially: p=1, r=n
if p < r
then q PARTITION(A, p, r)
QUICKSORT (A, p, q)
QUICKSORT (A, q+1, r)
Recurrence:
T(n) = T(q) + T(n – q) + f(n) PARTITION())
9
Partitioning the Array
• Choosing PARTITION()
– There are different ways to do this
– Each has its own advantages/disadvantages
• Hoare partition (Classical)
– Select a pivot element x around which to partition
– Grows two regions
A[p…i] x x A[j…r]
A[p…i] x
x A[j…r]
i j
10
Example
A[p…r] pivot x=5
5 3 2 6 4 1 3 7 5 3 2 6 4 1 3 7
i j i j
3 3 2 6 4 1 5 7 3 3 2 6 4 1 5 7
i j i j
A[p…q] A[q+1…r]
3 3 2 1 4 6 5 7 3 3 2 1 4 6 5 7
i j j i
11
Example
12
Partitioning the Array
Alg. PARTITION (A, p, r)
r
1. x A[p]
p
A: 5 3 2 6 4 1 3 7
2. i p – 1
3. j r + 1 i j
A[p…q] ≤ A[q+1…r]
4. while TRUE
5. do repeat j j – 1 A: ap ar
6. until A[j] ≤ x
7. do repeat i i + 1 j=q i
8. until A[i] ≥ x
Each element is
9. if i < j visited once!
10. then exchange A[i] A[j] Running time: (n)
n=r–p+1
11. else return j
13
Recurrence
Alg.: QUICKSORT(A, p, r) Initially: p=1, r=n
if p < r
then q PARTITION(A, p, r)
QUICKSORT (A, p, q)
QUICKSORT (A, q+1, r)
Recurrence:
T(n) = T(q) + T(n – q) + n
14
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
1 n-2 n-1
T(n) = T(1) + T(n – 1) + n,
n 1 n-3 n-2
T(1) = (1) 1
2 3
T(n) = T(n – 1) + n
1 1 2
n
n k 1 ( n ) ( n ) ( n )
2 2 (n2)
=
k 1
When does the worst case happen? 15
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)
16
Case Between Worst and Best
• 9-to-1 proportional split
Q(n) = Q(9n/10) + Q(n/10) + n
17
How does partition affect performance?
18
How does partition affect performance?
19
Performance of Quicksort
• Average case
– 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
partitioning cost:
n combined partitioning cost: n n = (n)
1 n-1 2n-1 = (n)
(n – 1)/2 + 1 (n – 1)/2
(n – 1)/2 (n – 1)/2
Alternate of a good Nearly well
and a bad split balanced split
• Running time of Quicksort when levels alternate
between good and bad splits is O(nlgn)
20
quicksort
21
partition
22
References
• Introduction to Algorithms, Third Edition
(International Edition), Thomas H. Cormen,
Charles E. Leiserson, Ronald L. Rivest, Clifford
Stein, ISBN-13: 978-0262033848. Page: 41-50
• Fundamentals of Computer Algorithms,
Second Edition, Ellis Horowitz, Sanguthevar
Rajasekaran, Sartaj Sahni, ISBN 10:
8173716129 / ISBN 13: 9788173716126,
Published by Universities Press/Orient
BlackSwan, 2008. Page: 29-49
References
• http://www.geeksforgeeks.org/sorting-algorithms/
• https://visualgo.net/en/sorting
• http://sorting.at/
• https://www.toptal.com/developers/sorting-
algorithms
• http://www.geeksforgeeks.org/quick-sort/
24
• Allah Hafez