Download presentation
Presentation is loading. Please wait.
Published byLinette Jacobs Modified over 9 years ago
1
CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2014W1 1
2
Today’s Outline Sorting with Priority Queues, Three Ways 2
3
Quick Review of Sorts Insertion Sort: Keep a list of already sorted elements. One by one, insert new elements into the right place in the sorted list. Selection Sort: Repeatedly find the smallest (or largest) element and put it in the next slot (where it belongs in the final sorted list). Merge Sort: Divide the list in half, sort the halves, merge them back together. (Base case: length 1.) 3
4
Insertion Sort Invariant 4 in sorted orderuntouched each iteration moves the line right one element
5
Selection Sort Invariant 5 smallest, 2 nd smallest, 3 rd smallest, … in order remainder in no particular order each iteration moves the line right one element
6
How Do We Sort with a Priority Queue? You have a bunch of data. You want to sort by priority. You have a priority queue. WHAT DO YOU DO? 6 F(7) E(5) D(100) A(4) B(6) insert deleteMin G(9)C(3)
7
“PQSort” (Super-Ridiculously Vague Pseudocode) Sort(elts): pq = new PQ for each elt in elts: pq.insert(elt); // or all at once if that’s easier sortedElts = new array of size elts.length for i = 0 to elts.length – 1: sortedElts[i] = pq.deleteMin // or all at once return sortedElts 7 What sorting algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these
8
Reminder: Naïve Priority Q Data Structures Unsorted array (or linked list): –insert: worst case O(1) –deleteMin: worst case O(n) Sorted array: –insert: worst case O(n) –deleteMin: worst case O(1) 8
9
“PQSort” deleteMaxes with Unsorted List MAX-PQ 9481610121323142075 123456789101112013 How long does inserting all of these elements take? And then the deletions…
10
“PQSort” deleteMaxes with Unsorted List MAX-PQ 9481610121323142075 123456789101112013 PQ
11
“PQSort” deleteMaxes with Unsorted List MAX-PQ 9481610121323142075 123456789101112013 9481610121323147205 PQ Result
12
“PQSort” deleteMaxes with Unsorted List MAX-PQ 9481610121323142075 123456789101112013 9481610121323147205 PQ Result 9481610121323714205 PQResult
13
“PQSort” deleteMaxes with Unsorted List MAX-PQ 13 9481610121323142075 123456789101112013 9481610121323147205 PQ Result 9481610121323714205 PQResult 9481610127231314205 PQResult
14
Two PQSort Tricks 1)Use the array to store both your results and your PQ. No extra memory needed! 2)Use a max-heap to sort in increasing order (or a min-heap to sort in decreasing order) so your heap doesn’t “move” during deletions. 14
15
“PQSort” deleteMaxes with Unsorted List MAX-PQ How long does “build” take? No time at all! How long do the deletions take? Worst case: O(n 2 ) What algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these 15 9481610127231314205 PQResult
16
“PQSort” insertions with Sorted List MAX-PQ 16 9481610121323142075 123456789101112013 PQ 9481610121323147205 PQ 9481610121323714205 PQ 9481610121323714205 PQ
17
“PQSort” insertions with Sorted List MAX-PQ 17 9481610121323714205 PQ How long does “build” take? Worst case: O(n 2 ) How long do the deletions take? What algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these
18
“PQSort” Build with Heap MAX-PQ 18 9481610121323142075 123456789101112013 14123610982145720 PQ Floyd’s Algorithm Takes only O(n) time!
19
“PQSort” deleteMaxes with Heap MAX-PQ 19 123456789101112013 14123610982145720 PQ 1310123679821452014 PQ 1210936758214142013 PQ 9108367542113142012 PQ Totally incomprehensible as an array!
20
“PQSort” deleteMaxes with Heap MAX-PQ 20 321312 10618 49 5 94816 121323142075 7 14
21
“PQSort” deleteMaxes with Heap MAX-PQ 21 321312 10618 49 5 720141289 106312 1413 20 754 Build Heap Note: 9 ends up being perc’d down as well since its invariant is violated by the time we reach it.
22
“PQSort” deleteMaxes with Heap MAX-PQ 1289 106312 1413 20 754 1289 76312 1013 14 54 20 1285 7639 1012 13 4 1420 1245 7638 109 12 131420 245 1638 79 10 12131420 42 1635 78 9 1012131420
23
“PQSort” with Heap MAX-PQ 23 How long does “build” take? Worst case: O(n) How long do the deletions take? Worst case: O(n lg n) What algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these 42 1635 78 9 1012131420 8753612410121314209 PQResult
24
“PQSort” Sort(elts): pq = new PQ for each elt in elts: pq.insert(elt); sortedElts = new array of size elts.length for i = 0 to elements.length – 1: sortedElts[i] = pq.deleteMin return sortedElts 24 What sorting algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these
25
To Do Read: Epp Section 9.5 and KW Section 10.1, 10.4, and 10.7-10.10 25
26
Coming Up More sorting! 26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.