



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Buell; Class: ALGORITHMIC DESIGN II; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Fall 2008;
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




You are to write a program to use an ArrayList to create a priority queue. The data records to be dealt with are simulated process identifiers pids such as might be managed by the operating system of a computer. The interface for the ProcessRecord class is found below. Note that a process has for these purposes at least a pid, a priority, and an increment all of which are integer values. The pid is the unique identifier given to the process by the operating system when the process is started. The priority is the current priority of the process and is the value on which you are to sort the ArrayList. The increment is a value that can be given to a process; this would be the increment to the process’s priority that would be applied the next time the operating system gets around to looking at priorities. In some operating systems, a process that was hogging memory or CPU might have its priority either raised (to help get the process out of the system and free up some resources) or lowered (to allow the large number of other processes to get through the system), and this increment value could be used to change the priority of a process. (In a real operating system, the data record that represented the process would have a number of other attributes, such as memory usage, files open, parent process, the login id of the user who fired up the process, and such. To see some of this in action, just log in to any Unix/linux system and run top to display some of the information. On a Windows machine, the task manager will display some of this other information.) Another way of looking at this assignment is that you are being asked to write an insertion sort. Using an ArrayList with an insertion sort to implement a priority queue is in fact one way to do a priority queue. Although it works, it’s not the best way to do a PQ, and a subsequent assignment will have you implement the better way using a heap. Your program should have three primary classes. The main program should do the usual things that we have been doing in this class. Besides opening files and etc., you need to little more than the main program fragment given below. That is, your real assignment is to read in process records and to insert them into an ArrayList in descending sorted order based on the value of key (which happens to be that of priority). The main program should create an instance of a priority queue class defined by the interface given below.
MAIN PROGRAM FRAGMENT theQ = new ArrayListPQ(); while(inFile.hasNext()) { rec = new ProcessRecord(); rec = rec.readProcessRecord(inFile); theQ.insert(rec); } // while(inFile.hasNext())
// print the Q so you know what it is FileUtils.logFile.printf("The Q of %d items is%n%s%n", theQ.getSize(),theQ.toString());
// now remove the elements one by one and print them // this should look just like the ’toString’ output above but // this will show that your removal works down to an empty Q while(theQ.getSize() > 0) { rec = new ProcessRecord(); rec = theQ.remove(); FileUtils.logFile.printf("Item is%s%n",rec.toString()); }
public interface IPQ { /*********************************************************************
/*********************************************************************
/***********************************************************
/***********************************************************
/*********************************************************************
The max method should return the record at subscript zero in the ArrayList but should not delete the record from the list. The remove method should return the record at subscript zero in the ArrayList and delete this record from the list. A hint on the insertion of records. This is essentially an insertion sort on priority. If you add the new record to the ArrayList then the record will automatically be placed at the end of the list and the list will automatically be made one longer than before. To put the new record in the proper location in the list you should now work your way from the end of the list to the beginning of the list, compare the i − 1-th and the i records, and swap them if the priority has them in the wrong order. Since you can assume you started with a sorted list, when you get to a point that the new record does not need to be swapped with an existing record, you can quit completely. Going from the end to the beginning allows you to terminate early. If you insert at the beginning, you will have to move the entire list down one slot, and this is wasteful.
You can demonstrate that your code works by extracting the values from the queue; when you do this, the values should come out in descending sorted order. You do not need to sort both by process id and by priority. The process id can be treated simply as a label, and this can be used in debugging to make sure that you are handling properly the items of equal priority.