CS221: Algorithms & Data Structures Lecture #3.5 Sorting Takes Priority (Sorting with PQs, Three Ways) Steve Wolfman 2010W2 1.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Warm Up Problem of the Day Lesson Presentation Lesson Quizzes.
Slide 1 Insert your own content. Slide 2 Insert your own content.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
1 Chapter 40 - Physiology and Pathophysiology of Diuretic Action Copyright © 2013 Elsevier Inc. All rights reserved.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Addition Facts
Year 6 mental test 10 second questions Numbers and number system Numbers and the number system, fractions, decimals, proportion & probability.
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
O X Click on Number next to person for a question.
© S Haughton more than 3?
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
Twenty Questions Subject: Twenty Questions
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Linking Verb? Action Verb or. Question 1 Define the term: action verb.
Squares and Square Root WALK. Solve each problem REVIEW:
Energy & Green Urbanism Markku Lappalainen Aalto University.
CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2011W2 1.
CISC 235: Topic 7 Priority Queues and Binary Heaps.
© 2012 National Heart Foundation of Australia. Slide 2.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Before Between After.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
1 Ke – Kitchen Elements Newport Ave. – Lot 13 Bethesda, MD.
1 Unit 1 Kinematics Chapter 1 Day
O X Click on Number next to person for a question.
How Cells Obtain Energy from Food
Foundations of Data Structures Practical Session #11 Sort properties, Quicksort algorithm.
CSCE 2100: Computing Foundations 1 The Tree Data Model
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
F00 pq 1 Priority Queues Review the abstract data type Priority Queues Review different implementation options.
Computer Science CS 330: Algorithms Priority Queues Gene Itkis.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2014W1 1.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Heaps (8.3) CSE 2011 Winter May 2018.
Bohyung Han CSE, POSTECH
Presentation transcript:

CS221: Algorithms & Data Structures Lecture #3.5 Sorting Takes Priority (Sorting with PQs, Three Ways) Steve Wolfman 2010W2 1

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

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

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

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

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

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

“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

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

“PQSort” insertions with Sorted List MAX-PQ PQ How long does “build” take? Worst case: O(n 2 )  How long do the deletions take? No time at all! What algorithm is this? a.Insertion Sort b.Selection Sort c.Heap Sort d.Merge Sort e.None of these

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

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

“PQSort” deleteMaxes with Heap MAX-PQ

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

“PQSort” deleteMaxes with Heap MAX-PQ

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

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

Coming Up More sorting! Programming Project #2 18