Computer Science 210 Lab 8: Priority Queues, Heaps, and Heap Sort
Due: November 18, 1997

Objectives and Overview: A priority queue is a queue whose elements are removed in order of their "priority" rather than the order in which they arrived. This strategy for removal allows elements to "jump ahead" of other elements that had arrived earlier. The "heap" is a convenient way to implement a priority queue, since its smallest element is always the root. This lab explores the use of heaps to implement priority queues. It asks you to write a heapSort method which is outlined on page 222 of your text. You should read up through section 12.4.2 in Chapter 12 of your text before doing this lab.

Part 1 - Exercise the SortDriver2 Program

On the CS210 (Tucker) server are five files that you should drag to the desktop: sortDriver2.java, VectorHeapSort.java, VectorMergeSort.java, Assert.java (that's in the Source folder), and Structure.zip. Build a project called Lab8yourname that looks like the one below:

Now run this program to sort, say, 10 integers using the mergeSort for which it is currently set up. Notice that there's a commented-out call to a heapSort method which has not yet been implemented. That is, if you commented out the mergeSort call instead, the heapSort will not do any sorting of the Vector data; it will just return the original values untouched.

Part 2 - Design and Implement the Heap Sort

Your main task in this lab is to complete the heapSort method whose skeleton is provided inside the VectorHeapSort.java file. You should consider using the VectorHeap implementation of priority queues, so that integers removed will appear in sorted order.

Test this new method using the SortDriver2 program to be sure that it works properly.

Part 3 - Exercise the Heap Sort and Explore Its Complexity

Using a slightly different run of this program for the input If a woodchuck could chuck wood (without the exclamation mark at the end), answer the following quesitons:

  1. Draw the binary tree representation of the heap that occurs when sorting 10 random integers. Your drawing should have the shape of a heap like those shown on pages 215 and 217 of your text..
  2. Draw the accompanying vector representation of this tree, in the spirit of the vectors shown on pages 215 and 217.
  3. Run the SortDriver2 program using the heapSort for lists of length 10, 100, 500, 1000, and 2000. Record the run time in milliseconds for each of these runs.
  4. Repeat the experiment in the previous question for the mergeSort.
  5. Based on your experimental results, what conclusions might you draw about the relative complexities of the heapSort and the mergeSort algorithms? Are these conclusions consistent with what you know theoretically about their complexities? Explain.

Lab 8 Deliverables:

Submit your Java program from Part 2 of this lab by dragging it to the Drop Box folder. Also hand in a hard copy listing of your program, along with your answers to the questions in Part 3.

Feel free to work on the programming part of this lab individually or in teams of two. But please complete the answers to the questions in Part 3 individually.