Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due,...
-
date post
21-Dec-2015 -
Category
Documents
-
view
217 -
download
1
Transcript of Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due,...
![Page 1: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/1.jpg)
Quicksort
CSC 172
SPRING 2004
LECTURE 10
![Page 2: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/2.jpg)
Reminders Project 2 (polynomial linked-list) is due, tomorrow
Wed, 18th 5PM Computer Science Office – 7th floor CSB
Read Chapter 8 (Sorting)Quicksort is important
O(n log n) – expected“in place”
Shell sort will be a lab, next weekPay special attention to sec 8.8 “lower bound”
Quiz next Tuesday – Midterm 2 weeks
![Page 3: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/3.jpg)
Quicksort
The basic quicksort algorithm is recursiveChosing the pivot
Deciding how to partition
Dealing with duplicates
Wrong decisions give quadratic run times run times
Good decisions give n log n run time
![Page 4: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/4.jpg)
The Quicksort AlgorithmThe basic algorithm Quicksort(S) has 4 steps
1. If the number of elements in S is 0 or 1, return
2. Pick any element v in S. It is called the pivot.
3. Partition S – {v} (the remaining elements in S) into two disjoint groups
L = {x S – {v}|x v}
R = {x S – {v}|x v}
4. Return the results of Quicksort(L) followed by v followed by Quicksort(R)
![Page 5: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/5.jpg)
![Page 6: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/6.jpg)
![Page 7: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/7.jpg)
Write The Quicksort AlgorithmThe basic algorithm Quicksort(S) has 4 steps
1. If the number of elements in S is 0 or 1, return
2. Pick any element v in S. It is called the pivot.
3. Partition S – {v} (the remaining elements in S) into two disjoint groups
L = {x S – {v}|x v}
R = {x S – {v}|x v}
4. Return the results of Quicksort(L) followed by v followed by Quicksort(R)
(assume partition(pivot,list) & append(l1,piv,l2))
![Page 8: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/8.jpg)
public static Node qsort(Node n) {
Node list = n;
if ((list == null) || (list.next == null) return list;
Comparable pivot = list.data;
list = list.next;
Node secondList = partition(pivot,list);
return append(qsort(list),pivot,qsort(secondList));
}
![Page 9: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/9.jpg)
Some ObservationsMultibase case (0 and 1)
Any element can be used as the pivot
The pivot divides the array elements into two groupselements smaller than the pivot
elements larger than the pivot
Some choice of pivots are better than others
The best choice of pivots equally divides the array
Elements equal to the pivot can go in either group
![Page 10: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/10.jpg)
Example
85 24 63 45 17 31 96 50
![Page 11: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/11.jpg)
Example
85 24 63 45 17 31 96 50
![Page 12: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/12.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
![Page 13: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/13.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
![Page 14: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/14.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
![Page 15: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/15.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
24 17 31 45
![Page 16: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/16.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
24 17 31 45
24 17 45
![Page 17: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/17.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
24 17 31 45
24 17 45
![Page 18: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/18.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
24 17 31 45
17 24 45
![Page 19: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/19.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
24 17 31 45
17 24 45
24
![Page 20: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/20.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
24 17 31 45
17 24 45
![Page 21: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/21.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
17 24 31 45
45
![Page 22: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/22.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
24 45 17 31 85 63 96
17 24 31 45
![Page 23: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/23.jpg)
Example
85 24 63 45 17 31 96 50
24 45 17 31 50 85 63 96
17 24 31 45 85 63 96
![Page 24: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/24.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
![Page 25: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/25.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
![Page 26: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/26.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
![Page 27: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/27.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
85 63
![Page 28: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/28.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
85 63
![Page 29: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/29.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
63 85
![Page 30: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/30.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
63 85
85
![Page 31: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/31.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
85 63 96
63 85
![Page 32: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/32.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 85 63 96
63 85 96
![Page 33: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/33.jpg)
Example
85 24 63 45 17 31 96 50
17 24 31 45 50 63 85 96
![Page 34: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/34.jpg)
Example
17 24 31 45 50 63 85 96
![Page 35: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/35.jpg)
Running Time
What is the running time of Quicksort?
Depends on how well we pick the pivot
So, we can look at
Best case
Worst case
Average (expected) case
![Page 36: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/36.jpg)
Worst case (give me the bad news first)
What is the worst case?
What would happen if we called Quicksort (as shown in the example) on the sorted array?
![Page 37: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/37.jpg)
Example
17 24 31 45 50 63 85 96
![Page 38: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/38.jpg)
Example
17 24 31 45 50 63 85 96
![Page 39: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/39.jpg)
Example
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85 96
![Page 40: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/40.jpg)
Example
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85
![Page 41: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/41.jpg)
Example
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85
![Page 42: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/42.jpg)
Example
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85
![Page 43: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/43.jpg)
Example
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85 96
17 24 31 45 50 63 85
17 24 31 45 50 63
How high will this tree call stack get?
![Page 44: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/44.jpg)
Worst Case
T(n) = T(n-1) + n
For the recursive call For the comparisonsin the partitioning
![Page 45: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/45.jpg)
Worst case expansion
T(n) = T(n-1) + n
T(n) = T(n-2) + (n-1) + n
T(n) = T(n-3) + (n-2) + (n-1) + n
….
T(n) = T(n-(n-1)) + 2 + 3 + … + (n-2)+(n-1) +n
T(n) = 1 + 2 + 3 + … + (n-2)+(n-1) +n
T(n) = n(n+1)/2 = O(n2)
![Page 46: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/46.jpg)
Best Case
Intuitively, the best case for quicksort is that the pivot partitons the set into two equally sized subsets and that this partitioning happens at every level
Then, we have two half sized recursive calls plus linear overhead
T(n) = 2T(n/2) + n
O(n log n)
Just like our old friend, MergeSort
![Page 47: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/47.jpg)
Best Case
More precisely, consider how much work is done at each “level”
We can think of the quick-sort “tree”
Let si(n) denote the sum of the input sizes of the nodes at depth i in the tree
![Page 48: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/48.jpg)
Example
15 7 9 3 13 5 11 2 14 6 10 1 12 4 8
![Page 49: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/49.jpg)
Example
15 7 9 3 13 5 11 2 14 6 10 1 12 4 8
![Page 50: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/50.jpg)
Example
7 3 6 2 5 1 4 8 15 9 13 11 14 10 12
![Page 51: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/51.jpg)
Example
7 3 6 1 5 2 4 8 15 9 13 11 14 10 12
15 9 13 11 14 10 127 3 6 1 5 2 4
![Page 52: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/52.jpg)
Example
7 3 6 1 5 2 4 8 15 9 13 11 14 10 12
15 9 13 11 14 10 127 3 6 1 5 2 4
![Page 53: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/53.jpg)
Example
7 3 5 1 6 2 4 8 15 9 13 11 14 10 12
3 1 2 4 7 5 6 9 11 10 12 15 13 14
![Page 54: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/54.jpg)
Example
7 3 5 1 6 2 4 8 15 9 13 11 14 10 12
3 1 2 4 7 5 6 9 11 10 12 15 13 14
3 1 2 7 5 6 9 11 10 15 13 14
![Page 55: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/55.jpg)
Example
7 3 5 1 6 2 4 8 15 9 13 11 14 10 12
3 1 2 4 7 5 6 9 11 10 12 15 13 14
3 1 2 7 5 6 9 11 10 15 13 14
![Page 56: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/56.jpg)
Example
7 3 5 1 6 2 4 8 15 9 13 11 14 10 12
3 1 2 4 7 5 6 9 11 10 12 15 13 14
1 2 3 5 6 7 9 10 11 13 14 15
![Page 57: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/57.jpg)
Example
7 3 5 1 6 2 4 8 15 9 13 11 14 10 12
3 1 2 4 7 5 6 9 11 10 12 15 13 14
5 6 7 9 10 11 13 14 151 2 3
13 159 115 71 3
![Page 58: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/58.jpg)
What is size at each level?
7 3 5 1 6 2 4 8 15 9 13 11 14 10 12
3 1 2 4 7 5 6 9 11 10 12 15 13 14
5 6 7 9 10 11 13 14 151 2 3
13 159 115 71 3
n
n-1
n-3
n-7
What is the general rule?
![Page 59: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/59.jpg)
Best Case, more preciselyS0(n) = n
S1(n) = n - 1
S2(n) = (n – 1) – 2 = n – (1 + 2) = n-3
S3(n) = ((n – 1) – 2) - 4 = n – (1 + 2 + 4) = n-7…
Si(n) = n – ( 1 + 2 + 22+ … + 2i-1) = n - 2i + 1
Height is O(log n)No more than n work is done at any one levelBest case time complexity is O(n log n)
![Page 60: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/60.jpg)
Average case QuickSort
Because the run time of quicksort can vary, we would like to know the average performance.
The cost to quicksort N items equals N units for the partitioning plus the cost of the two recursive calls
The average cost of each recursive call equals the average over all possible sub-problem sizes
![Page 61: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/61.jpg)
Average cost of the recursive calls
N
NTTTTRTLT
)1(...)2()1()0()()(
![Page 62: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/62.jpg)
Recurrence Relation
NN
NTTTTNT
)1(...)2()1()0(
2)(
2)1(...)1()0(2)( NNTTTNNT
2)1()2(...)1()0(2)1()1( NNTTTNTN
12)1(2)1()1()( NNTNTNNNT
NNTNNNT 2)1()1()(
![Page 63: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/63.jpg)
Telescoping
1
2)1(
1
)(
NN
NT
N
NT
NN
NT
N
NT 2
1
)2()1(
1
2
2
)3(
1
)2(
NN
NT
N
NT
3
2
2
)1(
3
)2(
TT……
![Page 64: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/64.jpg)
So,
1
11...
5
1
4
1
3
12
2
)1(
1
)(
NN
T
N
NT
2
5
1
11...
4
1
3
1
2
112
1
)(
NNN
NT
Nth Harmonic number is O(log N)
)log()( NNONT
![Page 65: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/65.jpg)
Intuitively
f(x)= 1/x
1 n
area =log(x)
2 3
1/21/3
![Page 66: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/66.jpg)
Picking the Pivot
A fast choice is important
NEVER use the first (or last) element as the pivot!
Sorted (or nearly sorted) arrays will end up with quadratic run times.
The middle element is reasonable x[(low+high)/2]
but there could be some bad cases
![Page 67: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/67.jpg)
Median of three partitioning
Take the median (middle value) of the
first,
last,
middle
![Page 68: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/68.jpg)
In place partitioningPick the pivotSwap the pivot with the last elementScanning
Run i from left to rightwhen i encounters a large element, stop
Run j from right to leftwhen j encounters a small element, stop
If i and j have not crossed, swap values and continue scanning
If i and j have crossed, swap the pivot with element i
![Page 69: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/69.jpg)
Example
8 1 4 9 6 3 5 2 7 0
Quicksort(a,0,9)
Quicksort(a,low,high)
![Page 70: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/70.jpg)
Example
8 1 4 9 6 3 5 2 7 0
![Page 71: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/71.jpg)
Example
8 1 4 9 6 3 5 2 7 0
![Page 72: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/72.jpg)
Example
8 1 4 9 0 3 5 2 7 6
![Page 73: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/73.jpg)
Example
8 1 4 9 0 3 5 2 7 6
i j
![Page 74: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/74.jpg)
Example
8 1 4 9 0 3 5 2 7 6
i j
![Page 75: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/75.jpg)
Example
2 1 4 9 0 3 5 8 7 6
i j
![Page 76: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/76.jpg)
Example
2 1 4 9 0 3 5 8 7 6
i j
![Page 77: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/77.jpg)
Example
2 1 4 9 0 3 5 8 7 6
i j
![Page 78: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/78.jpg)
Example
2 1 4 9 0 3 5 8 7 6
i j
![Page 79: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/79.jpg)
Example
2 1 4 9 0 3 5 8 7 6
i j
![Page 80: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/80.jpg)
Example
2 1 4 5 0 3 9 8 7 6
i j
![Page 81: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/81.jpg)
Example
2 1 4 5 0 3 9 8 7 6
i j
![Page 82: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/82.jpg)
Example
2 1 4 5 0 3 9 8 7 6
i j
![Page 83: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/83.jpg)
Example
2 1 4 5 0 3 9 8 7 6
i j
![Page 84: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/84.jpg)
Example
2 1 4 5 0 3 9 8 7 6
ij
![Page 85: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/85.jpg)
Example
2 1 4 5 0 3 6 8 7 9
ij
Now, Quicksort(a,low,i-1) and Quicksort(a,i+1,high)
![Page 86: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/86.jpg)
Java Quicksort
public static void quicksort(Comparable [] a) {
quicksort(a,0,a.length-1);
}
![Page 87: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/87.jpg)
public static void quicksort(Comparable [] a,int low, int high) {
if (low + CUTOFF > high) insertionSort(a,low,high);
else {
int middle = (low + high)/2;
if (a[middle].compareTo(a[low]) < 0) swap(a,low,middle);
if (a[high].compareTo(a[low]) < 0) swap(a,low,high); if (a[high].compareTo(a[middle]) < 0) swap(a,middle,high);
swap(a,middle,high-1);
Comparable pivot = a[high-1];
![Page 88: Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders Project 2 (polynomial linked-list) is due, tomorrow Wed, 18 th 5PM Computer Science Office –](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649d545503460f94a30e04/html5/thumbnails/88.jpg)
int i,j;
for (i=low;j=high-1;;) {
while(a[++i].compareTo(pivot) < 0) ;
while(pivot.compareTo(a[--j]) < 0) ;
if (i >= j) break;
swap(a,i,j);
}
swap(a,i,high-1);
quicksort(a,low,i-1);
quicksort(a,i+1;high);
}
}