Professional Documents
Culture Documents
Algo Quicksort PDF
Algo Quicksort PDF
Mumit Khan
Computer Science and Engineering BRAC University
References 1 2 T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, Second Edition. The MIT Press, September 2001. Erik Demaine and Charles Leiserson, 6.046J Introduction to Algorithms. MIT OpenCourseWare, Fall 2005. Available from: ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/ 6-046JFall-2005/CourseHome/index.htm Last modied: February 13, 2011
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Mumit Khan Licensed under CSE 221: Algorithms 1 / 18
Quicksort
Contents
Quicksort Introduction Partitioning Quicksort algorithm Quicksort analysis Randomized Quicksort Conclusion
Mumit Khan
Licensed under
2 / 18
Quicksort
Contents
Quicksort Introduction Partitioning Quicksort algorithm Quicksort analysis Randomized Quicksort Conclusion
Mumit Khan
Licensed under
3 / 18
Quicksort
Quicksort
Mumit Khan
Licensed under
4 / 18
Quicksort
Quicksort
Proposed by C. A. R. Hoare in 1962. Divide and Conquer algorithm like merge sort.
Mumit Khan
Licensed under
4 / 18
Quicksort
Quicksort
Proposed by C. A. R. Hoare in 1962. Divide and Conquer algorithm like merge sort. In-place algorithm like insertion and heap sorts.
Mumit Khan
Licensed under
4 / 18
Quicksort
Quicksort
Proposed by C. A. R. Hoare in 1962. Divide and Conquer algorithm like merge sort. In-place algorithm like insertion and heap sorts. Runs very well with tuning.
Mumit Khan
Licensed under
4 / 18
Quicksort
Quicksort
Proposed by C. A. R. Hoare in 1962. Divide and Conquer algorithm like merge sort. In-place algorithm like insertion and heap sorts. Runs very well with tuning. Worst-case is O(n2 ), but for all practical purposes runs in O(n lg n).
Mumit Khan
Licensed under
4 / 18
Quicksort
Quicksort
Proposed by C. A. R. Hoare in 1962. Divide and Conquer algorithm like merge sort. In-place algorithm like insertion and heap sorts. Runs very well with tuning. Worst-case is O(n2 ), but for all practical purposes runs in O(n lg n). Why do we want to study Quicksort? One of the most widely used, and extensively studied, sorting algorithms.
Mumit Khan
Licensed under
4 / 18
Quicksort
Divide Partition the array into subarrays around a pivot x such that the elements in lower subarray x elements in the upper subarray.
2 3
Conquer Recursively sort the two subarrays. Combine Trivial just concatenate the lower subarray, pivot, and the upper subarray.
Mumit Khan
Licensed under
5 / 18
Quicksort
Divide Partition the array into subarrays around a pivot x such that the elements in lower subarray x elements in the upper subarray.
2 3
Conquer Recursively sort the two subarrays. Combine Trivial just concatenate the lower subarray, pivot, and the upper subarray.
Mumit Khan
Licensed under
5 / 18
Quicksort
Divide Partition the array into subarrays around a pivot x such that the elements in lower subarray x elements in the upper subarray.
2 3
Conquer Recursively sort the two subarrays. Combine Trivial just concatenate the lower subarray, pivot, and the upper subarray.
Mumit Khan
Licensed under
5 / 18
Quicksort
Divide Partition the array into subarrays around a pivot x such that the elements in lower subarray x elements in the upper subarray.
2 3
Conquer Recursively sort the two subarrays. Combine Trivial just concatenate the lower subarray, pivot, and the upper subarray.
Mumit Khan
Licensed under
5 / 18
Quicksort
Divide Partition the array into subarrays around a pivot x such that the elements in lower subarray x elements in the upper subarray.
2 3
Conquer Recursively sort the two subarrays. Combine Trivial just concatenate the lower subarray, pivot, and the upper subarray.
Mumit Khan
Licensed under
5 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Quicksort in action
Mumit Khan
Licensed under
6 / 18
Quicksort
Contents
Quicksort Introduction Partitioning Quicksort algorithm Quicksort analysis Randomized Quicksort Conclusion
Mumit Khan
Licensed under
7 / 18
Quicksort
Partitioning algorithm
Algorithm partition(A, p, q) A[p . . q] 1 2 3 4 5 6 7 8 x A[p] pivot = A[p] i p for j p + 1 to q do if A[j] x then i i + 1 exchange A[i] A[j] exchange A[p] A[i] return i
Mumit Khan
Licensed under
8 / 18
Quicksort
Partitioning algorithm
Algorithm partition(A, p, q) A[p . . q] 1 2 3 4 5 6 7 8 x A[p] pivot = A[p] i p for j p + 1 to q do if A[j] x then i i + 1 exchange A[i] A[j] exchange A[p] A[i] return i
Invariant
Mumit Khan
Licensed under
8 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Partitioning in action
Mumit Khan
Licensed under
9 / 18
Quicksort
Contents
Quicksort Introduction Partitioning Quicksort algorithm Quicksort analysis Randomized Quicksort Conclusion
Mumit Khan
Licensed under
10 / 18
Quicksort
Quicksort algorithm
Mumit Khan
Licensed under
11 / 18
Quicksort
Quicksort algorithm
Algorithm quicksort(A, p, r ) A[p . . r ] 1 if p < r 2 then q partition(A, p, r ) 3 quicksort(A, p, q 1) 4 quicksort(A, q + 1, r ) Initial call quicksort(A, 1, n)
Mumit Khan
Licensed under
11 / 18
Quicksort
Contents
Quicksort Introduction Partitioning Quicksort algorithm Quicksort analysis Randomized Quicksort Conclusion
Mumit Khan
Licensed under
12 / 18
Quicksort
Mumit Khan
Licensed under
13 / 18
Quicksort
Mumit Khan
Licensed under
13 / 18
Quicksort
Mumit Khan
Licensed under
13 / 18
Quicksort
Mumit Khan
Licensed under
13 / 18
Quicksort
Mumit Khan
Licensed under
13 / 18
Quicksort
Mumit Khan
Licensed under
14 / 18
Quicksort
Mumit Khan
Licensed under
14 / 18
Quicksort
Mumit Khan
Licensed under
14 / 18
Quicksort
Mumit Khan
Licensed under
14 / 18
Quicksort
Mumit Khan
Licensed under
14 / 18
Quicksort
Mumit Khan
Licensed under
14 / 18
Quicksort
Mumit Khan
Licensed under
15 / 18
Quicksort
Mumit Khan
Licensed under
15 / 18
Quicksort
1 10
9 10 ?
Mumit Khan
Licensed under
15 / 18
Quicksort
1 10
9 10 ?
Mumit Khan
Licensed under
15 / 18
Quicksort
1 10
9 10 ?
T (n) = T (n/10) + T (9n/10) + (n) = (n lg n) Key observation Very close to worst-case produces (n lg n), not (n2 ). See text for details
Mumit Khan
Licensed under
15 / 18
Quicksort
1 10
9 10 ?
T (n) = T (n/10) + T (9n/10) + (n) = (n lg n) Key observation Very close to worst-case produces (n lg n), not (n2 ). How to ensure that we dont usually hit the worst-case?
Mumit Khan Licensed under CSE 221: Algorithms 15 / 18
Quicksort
Contents
Quicksort Introduction Partitioning Quicksort algorithm Quicksort analysis Randomized Quicksort Conclusion
Mumit Khan
Licensed under
16 / 18
Quicksort
Randomized Quicksort
Pick a random pivot and partition around it.
Mumit Khan
Licensed under
17 / 18
Quicksort
Randomized Quicksort
Pick a random pivot and partition around it. Pivot independent of input order, so no specic input produces worst-case behavior.
Mumit Khan
Licensed under
17 / 18
Quicksort
Randomized Quicksort
Pick a random pivot and partition around it. Pivot independent of input order, so no specic input produces worst-case behavior. The worst-case is determined only by the output of a random number generator.
Mumit Khan
Licensed under
17 / 18
Quicksort
Randomized Quicksort
Pick a random pivot and partition around it. Pivot independent of input order, so no specic input produces worst-case behavior. The worst-case is determined only by the output of a random number generator. randomized-partition(A, p, r ) A[p . . r ] 1 i random(p, r ) 2 exchange A[p] A[i] 3 return partition(A, p, r ) i = [p . . r ]
Mumit Khan
Licensed under
17 / 18
Quicksort
Randomized Quicksort
Pick a random pivot and partition around it. Pivot independent of input order, so no specic input produces worst-case behavior. The worst-case is determined only by the output of a random number generator. randomized-partition(A, p, r ) A[p . . r ] 1 i random(p, r ) 2 exchange A[p] A[i] 3 return partition(A, p, r ) randomized-quicksort(A, p, r ) 1 if p < r 2 then q randomized-partition(A, p, r ) 3 randomized-quicksort(A, p, q 1) 4 randomized-quicksort(A, q + 1, r )
Mumit Khan Licensed under CSE 221: Algorithms 17 / 18
i = [p . . r ]
Quicksort
Conclusion
One of the most widely used sorting algorithm. While it runs in O(n2 ) time in the worst-case, it runs in O(n lg n) time on the average. Runs almost twice as fast as merge-sort. Can be tuned substantially. Many programming language runtime libraries provide some variant of Quicksort (java.util.Arrays.sort() in Java, qsort() in C, etc).
Mumit Khan
Licensed under
18 / 18
Quicksort
Conclusion
One of the most widely used sorting algorithm. While it runs in O(n2 ) time in the worst-case, it runs in O(n lg n) time on the average. Runs almost twice as fast as merge-sort. Can be tuned substantially. Many programming language runtime libraries provide some variant of Quicksort (java.util.Arrays.sort() in Java, qsort() in C, etc). Questions to ask (and remember)
Mumit Khan
Licensed under
18 / 18
Quicksort
Conclusion
One of the most widely used sorting algorithm. While it runs in O(n2 ) time in the worst-case, it runs in O(n lg n) time on the average. Runs almost twice as fast as merge-sort. Can be tuned substantially. Many programming language runtime libraries provide some variant of Quicksort (java.util.Arrays.sort() in Java, qsort() in C, etc). Questions to ask (and remember) What are the worst, best and average case performances?
Mumit Khan
Licensed under
18 / 18
Quicksort
Conclusion
One of the most widely used sorting algorithm. While it runs in O(n2 ) time in the worst-case, it runs in O(n lg n) time on the average. Runs almost twice as fast as merge-sort. Can be tuned substantially. Many programming language runtime libraries provide some variant of Quicksort (java.util.Arrays.sort() in Java, qsort() in C, etc). Questions to ask (and remember) What are the worst, best and average case performances? Is it in-place?
Mumit Khan
Licensed under
18 / 18
Quicksort
Conclusion
One of the most widely used sorting algorithm. While it runs in O(n2 ) time in the worst-case, it runs in O(n lg n) time on the average. Runs almost twice as fast as merge-sort. Can be tuned substantially. Many programming language runtime libraries provide some variant of Quicksort (java.util.Arrays.sort() in Java, qsort() in C, etc). Questions to ask (and remember) What are the worst, best and average case performances? Is it in-place? Is it stable?
Mumit Khan Licensed under CSE 221: Algorithms 18 / 18