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

Slides:



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

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
and 6.855J Spanning Tree Algorithms. 2 The Greedy Algorithm in Action
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
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
0 - 0.
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 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Richmond House, Liverpool (1) 26 th January 2004.
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
PP Test Review Sections 6-1 to 6-6
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
ADTs unsorted List and Sorted List
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
ABC Technology Project
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
© S Haughton more than 3?
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
VOORBLAD.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Squares and Square Root WALK. Solve each problem REVIEW:
Do you have the Maths Factor?. Maths Can you beat this term’s Maths Challenge?
CS221: Algorithms & Data Structures Lecture #3.5 Sorting Takes Priority (Sorting with PQs, Three Ways) Steve Wolfman 2010W2 1.
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Test Review Sections 5-1 through 5-4.
SIMOCODE-DP Software.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
1 Atlantic Annual Viewing Trends Adults 35-54, Total TV, By Daypart Average Minute Audience (000) Average Weekly Reach (%) Average Weekly Hours Viewed.
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
A SMALL TRUTH TO MAKE LIFE 100%
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
How Cells Obtain Energy from Food
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
Traktor- og motorlære Kapitel 1 1 Kopiering forbudt.
CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2014W1 1.
Presentation transcript:

CS221: Algorithms and Data Structures Lecture #3.5 Sorting Takes Priority Steve Wolfman 2011W2 1

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

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

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)

“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

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

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

“PQSort” deleteMaxes with Unsorted List MAX-PQ PQ

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

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

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

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

“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

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

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