1 / 24

CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority

CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority. Steve Wolfman 2011W2. Today’s Outline. Sorting with Priority Queues, Three Ways. Quick Review of Sorts.

catrin
Download Presentation

CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS221: Algorithms and Data StructuresLecture #3.5Sorting Takes Priority Steve Wolfman 2011W2

  2. Today’s Outline • Sorting with Priority Queues, Three Ways

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

  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? F(7) E(5) D(100) A(4) B(6) deleteMin insert 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 • What sorting algorithm is this? • Insertion Sort • Selection Sort • Heap Sort • Merge Sort • 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)

  7. “PQSort” deleteMaxes with Unsorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 How long does inserting all of these elements take? And then the deletions…

  8. “PQSort” deleteMaxes with Unsorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ

  9. “PQSort” deleteMaxes with Unsorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ 5 9 4 8 1 6 10 12 13 2 3 14 7 20 PQ Result

  10. “PQSort” deleteMaxes with Unsorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ 5 9 4 8 1 6 10 12 13 2 3 14 7 20 PQ Result 5 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ Result

  11. “PQSort” deleteMaxes with Unsorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ 5 9 4 8 1 6 10 12 13 2 3 14 7 20 PQ Result 5 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ Result 5 9 4 8 1 6 10 12 7 2 3 13 14 20 PQ Result

  12. Two PQSort Tricks • Use the array to store both your results and your PQ. No extra memory needed! • 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.

  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(n2)  What algorithm is this? • Insertion Sort • Selection Sort • Heap Sort • Merge Sort • None of these 5 9 4 8 1 6 10 12 7 2 3 13 14 20 PQ Result

  14. “PQSort” insertions with Sorted List MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 PQ 5 9 4 8 1 6 10 12 13 2 3 14 7 20 PQ 5 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ 5 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ

  15. “PQSort” insertions with Sorted List MAX-PQ How long does “build” take? Worst case: O(n2)  How long do the deletions take? What algorithm is this? • Insertion Sort • Selection Sort • Heap Sort • Merge Sort • None of these No time at all! 5 9 4 8 1 6 10 12 13 2 3 7 14 20 PQ

  16. “PQSort” Build with Heap MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 5 9 4 8 1 6 10 12 13 2 3 14 20 7 Floyd’s Algorithm 20 13 14 12 3 6 10 9 8 2 1 4 5 7 PQ Takes only O(n) time!

  17. “PQSort” deleteMaxes with Heap MAX-PQ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 20 13 14 12 3 6 10 9 8 2 1 4 5 7 PQ 14 13 10 12 3 6 7 9 8 2 1 4 5 20 PQ 13 12 10 9 3 6 7 5 8 2 1 4 14 20 PQ 12 9 10 8 3 6 7 5 4 2 1 13 14 20 PQ Totally incomprehensible as an array!

  18. “PQSort” deleteMaxes with Heap MAX-PQ 5 9 4 8 1 6 10 12 13 2 3 14 20 7 5 9 4 8 1 6 10 12 13 2 3 14 20 7

  19. “PQSort” deleteMaxes with Heap MAX-PQ 5 20 9 4 13 14 8 1 6 10 12 3 6 10 12 13 2 3 14 20 7 9 8 2 1 4 5 7 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 13 14 20 12 10 13 10 9 3 6 7 12 3 6 7 13 14 20 14 20 5 8 2 1 4 9 8 2 1 4 5 12 3 6 10 9 10 12 8 7 9 7 9 10 9 8 2 1 4 5 7 5 3 6 1 8 3 6 1 8 3 6 7 10 12 13 14 20 12 13 14 20 13 14 20 2 4 5 4 2 5 4 2 1

  21. “PQSort” with Heap MAX-PQ 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? • Insertion Sort • Selection Sort • Heap Sort • Merge Sort • None of these 9 8 7 5 3 6 1 10 12 13 14 20 2 4 9 8 7 5 3 6 1 2 4 10 12 13 14 20 PQ Result

  22. “PQSort” • What sorting algorithm is this? • Insertion Sort • Selection Sort • Heap Sort • Merge Sort • None of these 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

  23. To Do • Homework! • Read: Epp Section 9.5 and KW Section 10.1, 10.4, and 10.7-10.10

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

More Related