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

Lec8 1-Quicksort

Uploaded by

Rishabh Gupta
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)
19 views

Lec8 1-Quicksort

Uploaded by

Rishabh Gupta
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/ 21

Quicksort

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.
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 Time Extra 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)
Example of Partition
• If pk i A[k]  x
• If i+1  k  j-1  A[k] > x
• If k=r, A[k] = x
• If j  k  r-1  un-restricted
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
//lager 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 log10 n   (lg n)
9
– 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)
Analysis of Quicksort
Worst case
• The most unbalanced one --- 𝑂(𝑛2 )
Expected time complexity
• 𝑂(𝑛 𝑙𝑔𝑛)
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
𝑇 𝑛 = max 𝑇 𝑞 +𝑇 𝑛−𝑞−1 + 𝜃(𝑛)
0≤𝑞≤𝑛−1
Proof that 𝑇 𝑛 = 𝑂 𝑛2
1. When n=1, 𝑇 1 = 𝑇 0 + 𝑇 0 + 𝜃(1)= 𝑂 𝑛2
2. Hypothesis: when 𝑘 ≤ 𝑛, 𝑇 𝑘 = 𝑂 𝑘 2 ;
induction statement: when 𝑘 = 𝑛 + 1, 𝑇 𝑘 =
𝑂 𝑘2 ;
Strict proof of the worst case time
complexity
𝑇 𝑘 = 𝑇(𝑛 + 1)
= max 𝑇 𝑞 + 𝑇 𝑛 − 𝑞 + 𝜃(𝑛 + 1)
0≤𝑞≤𝑛
Since 𝑞 ≤ 𝑛, 𝑛 − 1 ≤ 𝑛,
𝑇 𝑘 ≤ max (𝑐𝑞2 + 𝑐 𝑛 − 𝑞 2 ) + 𝜃 𝑛 + 1
0≤𝑞≤𝑛
= 𝑐 max 𝑞 2 + 𝑛−𝑞 2
+ 𝜃 𝑛+1
0≤𝑞≤𝑛
≤ 𝑐 𝑛 + 𝜃 𝑛 + 1 = 𝑐(𝑘 − 1)2 + 𝜃 𝑘
2

= 𝑂 𝑘2 ;
Strict proof of the expected time
complexity
• Given A[1, …, n], after sorting them to
𝐴 1 ≤ 𝐴 2 … ≤ 𝐴 𝑛 , the chance for 2
elements, A[i] and A[j], to be compared is
2
.
𝑗−𝑖+1
• The total comparison is calculated as:
𝑛−1 𝑛 2
• 𝑖=1 𝑗=𝑖+1 𝑗−𝑖+1 = 𝑂(𝑛𝑙𝑔𝑛)

You might also like