Lec8 1-Quicksort
Lec8 1-Quicksort
• 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 Do nothing
2 8 7 1 3 5 6 4 Do nothing
Partition
• Partition example
i
2 1 3 8 7 5 6 4 Do nothing
i
2 1 3 8 7 5 6 4 Do nothing
T(n)=O(n)
Example of Partition
• If pk 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].
= 𝑂 𝑘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 = 𝑂(𝑛𝑙𝑔𝑛)