0% found this document useful (0 votes)
33 views20 pages

Lec9 Quicksort

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)
33 views20 pages

Lec9 Quicksort

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/ 20

Quicksort

Review of insertion sort and merge sort

• Insertion sort
– Worst case number of comparisons = O(?)
• Selection sort
– Worst case number of comparisons = O(?)
• Merge sort
– Worst case number of comparisons = O(?)
Sorting Algorithms
Algorithm Worst Time Expected Extra
Time Memory
Insertion sort 𝑂(𝑛2 ) 𝑂(𝑛2 ) O(1)
Selection sort 𝑂(𝑛2 ) 𝑂(𝑛2 ) O(1)
Merge sort 𝑂(𝑛 𝑙𝑔𝑛) 𝑂(𝑛 𝑙𝑔𝑛) O(n)
Quick sort 𝑂(𝑛2 ) 𝑂(𝑛 𝑙𝑔𝑛) O(1)
Heap sort 𝑂(𝑛 𝑙𝑔𝑛) 𝑂(𝑛 𝑙𝑔𝑛) O(1)
Quicksort Algorithm
• Input: A[1, …, n]
• Output: A[1, .., n], where A[1] ≤ A[2]…≤ A[n]
• Quicksort:
1. if(n ≤ 1) return;
2. Choose the pivot p = A[n]
3. Put all elements less than p on the left; put all elements
lager than p on the right; put p at the middle. (Partition)
4. Quicksort(the array on the left of p)
5. Quicksort(the array on the right of p)
Partition
• Partition example
i

2 8 7 1 3 5 6 4
i

2 8 7 1 3 5 6 4 Exchange 2 with A[i+1]


i

2 8 7 1 3 5 6 4 Do nothing

2 8 7 1 3 5 6 4 Do nothing
Partition
• Partition example
i

2 8 7 1 3 5 6 4 Exchange 1 with A[i+1]

2 1 7 8 3 5 6 4 Exchange 3 with A[i+1]


i

2 1 3 8 7 5 6 4 Do nothing
i

2 1 3 8 7 5 6 4 Do nothing

2 1 3 4 7 5 6 8 Final step: exchange A[n]


with A[i+1]
Partition

T(n)=O(n)
Quicksort Algorithm
• Quicksort example Current pivots

Previous pivots
2 8 7 1 3 5 6 4
Quicksort
2 1 3 4 7 5 6 8

2 1 3 4 7 5 6 8

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8
Quicksort Algorithm

Quicksort(A, p, r){
if(p<r){ // if(n<=1) return
q = partition(A, p, r) //small ones on left
//larger ones on right
Quicksort(A, p, q-1)
Quicksort(A, q+1, r)
}
}
Sorting the entire array: Quicksort(A, 1, length(A))
Analysis of Quicksort

Time complexity
– Worst case
– Expected
– Best case
Worst-Case Analysis
• What will be the worst case?
– Occur when PARTITION produces one subproblem with n-1 elements and
one with 0 elements
• When the input array is already sorted increasingly or decreasingly
– The pivot is the smallest element, all the time
– Partition is always unbalanced
Best-case Analysis
• What will be the best case?
– Partition is perfectly balanced.
– Pivot is always in the middle (median of the array)
Worst-case and Best-case Partitioning
• The behavior of quicksort is determined by the relative ordering
of the values in the array
• Worst case
– Occur when PARTITION produces one subproblem with n-1 elements
and one with 0 elements
• When the input array is already sorted increasingly or decreasingly
– T(n) = T(n-1) + T(0) + Θ(n) = T(n-1) + Θ(n)
• By substitution: T(n) = Θ(n2)
• Best case – one of size ⎣n/2⎦, and one of size ⎡n/2⎤ -1
– T(n) ≤ 2T(n/2) + Θ(n) T(n) = O(n lg n)
Balanced Partitioning
• The average-case running time of quicksort is much
closer to the best case than to the worst case
– Suppose T(n) ≤ T(9n/10) + T(n/10) + cn
• By recursion tree: O(n lg n)
– Every level of the tree has cost cn
– Boundary condition reaches at depth log10n= Θ(lg n)
– Recursion terminates at depth
– Even a 99-to-1 split yields an O(n lg n) running time
• Any split of constant proportionality yields a recursion tree of
depth Θ(lg n), where the cost at each level is O(n)
Improved Pivot Selection
➢ Pick median value of three elements from data array:

➢ A[0], A[n/2], and A[n-1]


➢ Use this median value as pivot.
Strict proof of the worst-case time complexity


Strict proof of the worst-case time complexity


Strict proof of the expected time complexity


• Slides and figures have been collected from various publicly available Internet
sources for preparing the lecture slides of IT2001 course. I acknowledge and thank
all the original authors for their contribution to prepare the content.

You might also like