Download presentation

Presentation is loading. Please wait.

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

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

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

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

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

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

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

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

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

10
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

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

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

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

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google