CS 367 - Introduction to Data Structures - Section 3

Written Assignment One: Sorting with Priority Queues
Due Tuesday, November 4, 1997 at 2:30pm

Contents


The Assignment

This assignment is based on the implementation of priority queues with arrays (as given in class), available on-line here.
  1. Write C++ code (or pseudocode) for a sort function that uses priority queues and has the following interface:
    template <class Item>
    void pqSort(Item A[], size_t n);
    // Precondition: The number of items in 
    // array A is given by n.
    // Postcondition: A is now sorted in 
    // decreasing order.
        
    If you choose to write pseudocode, make sure it is easy for me to see how your pseudocode would be implemented in C++. If you write in C++, use correct syntax, and use comments describing what is going on. In either case, make sure it is clear to what you are doing. You need not check the precondition.


  2. Explain what restrictions, if any, apply to the template parameter Item. (For example, does it require a copy constructor? Will it work for C++ builtin types? etc.)


  3. Give big-O analysis for your sort function. You must explain how you arrived at your result. You need not be formal. You may assume that the complexity of the priority queue operations are as follows:

    insert - O(1)
    maximum - O(1)
    extractMax - O(n)
    
    Make your bound as "tight" as possible.

    If your algorithm uses two loops, do not simply say "it uses two loops therefore it is O(n2)". Be precise: how many iterations does each loop take? Does one loop perform a number of iterations that is dependent on the status of the other loop?

    Be clear. You need not write a lot. Do not employ subterfuge - I mean it.


  4. What would the run-time of your algorithm be if the complexity of the priority queue operations were as follows:
    insert - O(lg n)
    maximum - O(1)
    extractMax - O(lg n)
    
    In this case, just give a big-oh answer and in one or two sentences explain why it is or isn't the same as in the previous question.

This is not a difficult assignment. It should not take you a long time. However, I expect everyone to do well on it. As incentive, if everyone does well, I will cut back on future O-notation questions. Otherwise, it's big-oh until the end of our days together...

Hint: You might want to implement your sort algorithm and test it out. This can help for determining whether your idea is correct and for analyzing the run-time complexity. All the files you need (including a test program) to support this can be found here. You just need to fill in the indicated region in the file pq-sort.C. If you'd like to see what the test-sort program does you can find it at ~cs367-3/Solutions/HW1/test-sort


Grading

The assignment will be graded (by the instructor) on an 100 point scale.

For more information on general grading policy, click here.


Submitting

Turn in a written (hard-copy) solution at the beginning of class on the day it is due.


Late Policy

The assignment is due on Tuesday, November 4 at the start of class. However, I will accept late assignments up until Thursday, November 6 at the start of class, with no penalty. No assignments will be accpeted after that time.