CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Stacks And Queues Chapter 18.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Topic 16 Queues "FISH queue: n. [acronym, by analogy with FIFO (First In, First Out)] ‘First In, Still Here’. A joking way of pointing out that processing.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Review Array Array Elements Accessing array elements
Algorithm and Data Structure Part II Dr. Naimah Yaakob
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Topic 16 Queues "FISH queue: n.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Presentation transcript:

CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Queues Priority Queues

Queues and Stacks Let's implement a queue with an array –what will be our instance variables?

Queues and Stacks Let's implement a queue with an array –what will be our instance variables? –if we want to keep a fixed sized array and we don't want to keep shifting the values around in the array we can use a scheme that stores an index to the front, an index to the rear –if we want the front to be the index that we would dequeue from and rear to be the index of the last element (so that we would enqueue to rear+1) then we should do the following: –front and rear being the same value means we have 1 element in the queue, so for an empty queue we would want front and rear start off at 0 and -1 –when enqueue first one, front stays 0 and rear becomes 0 –also, when rear goes off the end of the array we can wrap it around to 0, only if front is not still 0. Make sense?

Queues and Stacks Let's implement a queue with an array –how would we tell if the queue is full? when front is 0 and rear is maxindex or when...

Queues and Stacks Let's implement a queue with an array –how would we tell if the queue is full? when front is 0 and rear is maxindex or when front is one more than rear (but they are not 0 and -1)‏ –When dequeuing, we must be aware that if we are about to make the queue empty, then we should set front to 0 and rear to -1. –To test if the queue is empty, we will simply see if front is 0 and rear is -1. –When we enqueue what do w e need to be concerned with?

Queues and Stacks Let's use that Queue class in an interesting sorting method called Radix Sort.

Queues and Stacks Radix Sort. –consider the job of sorting (base 10) integers let's limit them to 0-99 for now (all >=0 and <= 100)‏ –we handled this in several ways already –a totally different way is the following way start with an unsorted list separate the numbers to be sorted into 10 different bins based on their 1's digit then, get the numbers out of the bins and make a new list (take numbers from bin 0, then add on to the end of the list the numbers in bin 1,... and so on, finally adding to the end of the list the numbers from bin 9)‏ then separate this list into 10 bins based on the 10's digit. get the numbers out of the bins like before to get a sorted list.

Queues and Stacks Radix Sort. –example on board with the following: { 12, 15, 88, 76, 63, 21, 22, 1, 52, 2, 23, 5 }

Queues and Stacks Radix Sort. –example on board with the following: { 12, 15, 88, 76, 63, 21, 22, 1, 52, 2, 23, 5 } –each of the 10 bins were queues –also if you want to sort numbers that have a maximum of n digits in them, then you need n passes through the sort and each pass is a higher place in the number –we could use an array of 10 queues each index to the array corresponds to the digit in whatever place we're working on

Priority Queues Priority queues have the following characteristics –Each item placed into a priority queue has a priority value associated with it –When a remove is requested from a priority queue, we remove the highest priority item –We should also have a way to determine if the priority queue is empty –We can also have a peek to see what item has the highest priority without removing it It is best to limit priority values to be integers.

Priority Queues It is also a wise thing to maintain the following values for a priority queue –current size (how many items are in the queue)‏ –the priority of the highest priority item in the queue to make remove more efficient however it causes (some) additional work –after the remove happens we need to possibly change this value –after an add we just need to check if the one we added is higher than the highest, if so, change it.

Priority Queues Implementation of a priority queue could be an array of queues as well. The range of priority values (all integers) determines how many queues we're storing --- that is, how many elements of the array there will be. Let's implement a priority queue in this way now.