CS 342 Lab 6: Observable Sort



Lab date: 14 March-23 March 2000
Due date: Friday 24 March 2000 11:59 pm


Build a sort program with the following features:
  1. It sorts Arrays of elements. Use your Array implementation from Lab 2. You can assume that the type with which the Array is instantiated supports the less-than (<) operation, and supports shifting to/from ostreams/istreams.

  2. Unsorted (and sorted) data in the Array are encapsulated in a Sort_Info<T> object.

  3. For this lab, you only need to implement selection sort. But, your sorter must allow for expansion (in the next lab) to support additional sort algorithms. The sort algorithm can be chosen by the user, so it is encoded in the Sort_Info<T> object.

  4. A Sorter object sorts the data in each Sort_Info<T>, then places the output in the Sort_Info<T>.

  5. Observers can register with the Sorter to see the results of, and progress of, a sort. Observers can use these sort results in one of several forms, such as graphically or with simple text.

Here's a high-level diagram of the sort program collaborators:


  1. Review the Sorter interface (Sorter.h) that is part of the distribution for this lab.

  2. Review the header file for the Sort_Info template class (Sort_Info.h) that is part of the distribution for this lab.

  3. Review the Sort_Observer interface (Sort_Observer.h) that is part of the distribution for this lab. See the Sort Viewer section, below, for information on using the Sort Viewer.

  4. Review the patterns used in this lab, including


  1. Implement the Sort_Info template class, based on the interface supplied.

  2. Implement a selection sort class, which will encapulate a selection sort operation on a Sort_Info<T> object.

  3. Implement your Sorter. It must:
    1. Implement the interface, which accepts Sort_Info<T> requests and returns Sort_Info<T> responses.
    2. Delegate each sort request to the selection sort component.

  4. After completing the steps above, add your interaction to the Sort Viewer. This should simply be a matter of adding calls to the Sort_Observer interface to your selection sort implementation.

NOTE: this Lab should involve fewer of the intricate C++ details that we've encountered in previous labs. However, it does require more planning and design work up front. Please allow sufficient time for that. If you haven't completed your design and started the implementation by the end of the first week, you probably won't be able to complete the lab by the end of the second week.

The Sort_Info_Base class declares a few enums. enums are separate types, like classes. But, they can only have a limited number of values, which are specified in the enum declaration. enums are represented with integers (or something smaller), and can be promoted to (used as) integers.

Shifting enums from istreams is a bit tricky. The course C++ I/O slides (gzip-ps) (pdf) show a couple of ways to do it; see slides 14-16. You can use either approach for Lab 6.

You can use your Sort_Info_Base::print () and Sort_Info_Base::read () methods in the derived (Sort_Info<T>) class, even though they're pure virtual. Just call them explicitly, using the Sort_Info_Base:: prefix.

Obtaining the Lab 6 distribution:

At a Unix shell prompt, type ~cs342/bin/lab6. That will copy several files to a new lab6 subdirectory. You can cd lab6 and enter make to compile the code, if you like. However, it will not build until your implementation is filled in.

NOTE: You can use the distribution mechanism to revert one or more of your files at any time. First, delete the file (or better, mv it to a file with a different name, such as Sorter.cc.BAD). Then, run cvs update to update your workspace. That will replace any missing files. It will also let you know of any files that you modified but did not delete. cvs is in pkg gnu, in case you don't find it at first.

After obtaining the Lab 6 distribution, copy your Array.h and Array.cc files from your Lab 2 or 5 workspace.

What to Submit:

By the due date, submit all source files from your final solution. Include a laboratory writeup file (named readme) documenting what you did to satisfy this assignment. readme contains a minimum list of sections that you must provide. (Please replace the comments in [] with your descriptions.) These files will be submitted automatically when you execute the command:
make turnin
NOTE: there is a Makefile target that allows you to test what you are going to turn in:
make test_turnin

It places the output that will be turned in into the TEST_TURNIN directory. You are responsible for using make test_turnin, and verifying that the files that you will submit are correct.

Sort Viewer

A Java Sort Viewer is included in the Lab 6 distribution. It includes a C++ interface, Sort_Viewer_Client. To use:
  1. Add calls in your code to the functions in Sort_Viewer_Client.cc. It would be best to write a small adapter class to insulate the direct calls, and to make it easier to shutdown the Sort_Viewer_Client connection to the Sort_Viewer.

  2. Build and run the Sort_Viewer:
        % pkgadd java_1.1.7
        % javac Sort_Viewer.java
        % java Sort_Viewer &

Then, run your main as usual.

Please see the Lab 4 assignment for notes on using the History class to help track and count instantiations and deletions of an instrumented class.

Please see the Lab 2 assignment for hints on compiling templates.

CS 342 home page