Due Tuesday, April 3, 2001 at 11:59
PM
In this lab you will implement various sorting algorithms, including a
graphical subsystem to help visualize the sorting process.
See Prof.
Wong's SIGCSE 2001 materials for more information, documentation and demos.
Merritt Sorting Taxonomy
In her paper "An Inverted Taxonomy of Sorting
Algorithms," (Communications of the ACM, Jan. 1985, Vol. 20 No. 1, pp.
96-99), Susan Merritt describes how all binary comparison sorters can be
classified according to how they implement a "divide-and-conquer"
technique of splitting and joining. Merritt claims that
all sorters follow the following recursive execution
pattern:
- Split the array into two sub-arrays.
- Sort one sub-array.
- Sort the other sub-array.
- Join the two sorted sub-arrays back into the orginal size
array.
While Merritt's paper does not describe the process in terms of design
patterns, it is a perfect match for the Template design pattern.
Template Pattern for Sorting:
In the Template design pattern , a concrete
public method in the abstract class calls abstract
non-public methods in a particular
sequence to complete a task. The concrete method is
a "template" for the process. How the actual sub-chores
are performed is determined by the concrete
sub-classes while the overall process is controlled by the abstract
class. All the sub-classes need to do is to
implement the abstract methods called by the template method. Thus the
template method is a invariant behavior
which calls variant methods of the sub-classes.
One can think of the concrete classes as providing "services" to the
abstract class in a manner very similar to how components of a
framework system provide services to the framework.
The abstract sorter, AComparableSorter, has an invariant sorting behavior that manifests
itself as a concrete sort() method. This method has the
following steps:
Note:
An array is defined as all the elements from index "lo" to
index "hi" inclusive. This is denoted by "[lo,
hi]".
Split() returns the index "s" such that the first subarray is
defined from [lo, s-1] and the second subarray is defined from [s,
hi].
Join() combines the two subarrays defined by [lo,s-1] and [s,
hi].
public void sort(
IComparable[] array, int lo, int
hi)
{
if(lo<hi) // only sort arrays with 2 or more
elements! {
int s = split(array, lo,
hi);
sort(array, lo,
s-1);
sort(array, s,
hi);
join(array, lo, s,
hi);
}
}
where split() and join() represent variant
behaviors and are thus
manifested as abstract methods.
Plan of Attack
Your program should have all the features of the
on-line demo
except HeapSort and ShakerSort.
Note that some of the classes are supplied for you
in the CS151 FTP download site in the Sorter package:
ftp://exciton.cs.oberlin.edu/CS151
Some
additional docuementation can be found here.
To orient you as to what is required when:
For week 1: All the
required algorithms must work with an automatically updating text area display.
For week 2: Full
functionality of the on-line demo.
Remember, get small pieces working one
at a time!
Here is a recommended process to get
the lab working:
- First program the ability to create the data to
be sorted and to automatically display the contents of the data arary:
- Add a Build button that instantiates an array
Objects filled with
CIntegersin numerical
order. The size of the array should be determined
from a text field initialized to a default value.
- Add the JTextArea on which to display the data with one value per
line.
- Print the array after building it.
- Modify the build procedure to build a random array with no repeated
values (see Section 1A
):
- Fill the array with values which equal
their array index.
- Go throught the array and swap each element with some randomly
chosen other element.
- Add the simple animation of the values of the array in the JTextArea.
- Add the Timer and ActionListener.
- Write the ArrayMapper and A Command classes.
- Use and ArrayMapper and ACommand to display the data array every tick of
the Timer.
- Be sure to remove the manual update of the text area from
the Build button.
- Add a Sort button that uses the ASorter to sort the array.
- Add one radio
button:
- Initializes an ASorter variable to a simple concrete sorter that simply prints
something during a split or a join.
- Test your system before going on.
- Make a SelectionSorter
and test it.
- Modify the Sort button to start the sorter on its own thread.
- Modify the Selection Sorter radio button so that the AOrder object is obtained via a factory
method. This will enable us to dynamically modify
(decorate!) the ordering strategy next week.
- Add the rest of the sorters.
Note that the new JBuilder has the ability to add the ButtonGroup from the main component
palette. It is a non-visual component, but once you add it,
a radio button can be set to use it. No more adding the button group by hand!
- You have no chance to sort make your time.
Requirements for this week (Lab 7):
Your program for this week should be able to
-
Construct an array filled with sequential
CIntegers but in random order.
-
The number of elements in the array should be
adjustable at run time.
-
The sorting should run on its own thread.
-
Sort the array using all the sorters in
Section 1.
-
Automatically display the data in a text
area.
Requirements for next week (Lab 8):
Essentially, in the end your program should have
all the capabilities of the SIGCSE 2001
demo. Specifically, your program for next week should add the
following capabilities:
-
Graphically show the array being
sorted.
-
The data should be graphed as either dots
or bars (selectable at runtime).
-
Check boxes to enable
-
The objects being compared to show up as
a distinct color and for the sorting to pause for a user
definable amount of time. The compare color should be
selectable.
-
The arrays being split off to change to different
colors and when joined back together, for the
color to be returned to the original color (to
within round-off errors). When the array is being split or
joined, the sorting should pause for a user definable amount of
time.
-
The number of compares performed during the
sort to be displayed.
-
The number of accesses to the data i.e.
number of times getValue() is called, to be
displayed.
-
The order of sorting to be
reversed.
|