Download presentation

Presentation is loading. Please wait.

Published byTre Bewley Modified over 2 years ago

1
CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2011W2 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. Merge Sort: Divide the list in half, sort the halves, merge them back together. (Base case: length 1.) 3

4
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? 4 F(7) E(5) D(100) A(4) B(6) insert deleteMin G(9)C(3)

5
“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 elts.length – 1: sortedElts[i] = pq.deleteMin return sortedElts 5 What sorting algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these

6
Reminder: Naïve Priority Q Data Structures Unsorted list: –insert: worst case O(1) –deleteMin: worst case O(n) Sorted list: –insert: worst case O(n) –deleteMin: worst case O(1) 6

7
“PQSort” deleteMaxes with Unsorted List MAX-PQ How long does inserting all of these elements take? And then the deletions…

8
“PQSort” deleteMaxes with Unsorted List MAX-PQ PQ

9
“PQSort” deleteMaxes with Unsorted List MAX-PQ PQ Result

10
“PQSort” deleteMaxes with Unsorted List MAX-PQ PQ Result PQResult

11
“PQSort” deleteMaxes with Unsorted List MAX-PQ PQ Result PQResult PQResult

12
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. 12

13
“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 PQResult

14
“PQSort” insertions with Sorted List MAX-PQ PQ PQ PQ PQ

15
“PQSort” insertions with Sorted List MAX-PQ 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

16
“PQSort” Build with Heap MAX-PQ PQ Floyd’s Algorithm Takes only O(n) time!

17
“PQSort” deleteMaxes with Heap MAX-PQ PQ PQ PQ PQ Totally incomprehensible as an array!

18
“PQSort” deleteMaxes with Heap MAX-PQ

19
“PQSort” deleteMaxes with Heap MAX-PQ Build Heap Note: 9 ends up being perc’d down as well since its invariant is violated by the time we reach it.

20
“PQSort” deleteMaxes with Heap MAX-PQ

21
“PQSort” with Heap MAX-PQ 21 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 PQResult

22
“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 22 What sorting algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these

23
To Do Homework! Read: Epp Section 9.5 and KW Section 10.1, 10.4, and

24
Coming Up More sorting! Midterm (Feb 15 th ) 24

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google