Take a moment to view the Insertion Sort and Merge Sort interactive visualization programs written by David Galles at the University of San Francisco.
For today’s lab, we’ll take Weiss’s implementation of mergesort
as a starting point.
It can be found on MathLAN on lines 97–157
of the file
/home/jimenezp/csc207/Lab-Sorting/Sort.java
.
To test your program you will need the file
/home/jimenezp/csc207/Lab-Sorting/eight-cousins.txt.tokenized
Open up Eclipse and create a new Java project for this lab.
MergeSorter
class, and copy the definitions (implementations) of the three methods that make up Weiss’s implementation of mergesort into your MergeSorter class file. The three before mentioned methods are:
public static <AnyType extends Comparable<? super AnyType>>
void mergeSort( AnyType [ ] a )
private static <AnyType extends Comparable<? super AnyType>>
void mergeSort( AnyType [ ] a, AnyType [ ] tmpArray, int left, int right)
private static <AnyType extends Comparable<? super AnyType>>
void merge( AnyType [ ] a, AnyType [ ] tmpArray, int leftPos, int rightPos, int rightEnd )
type parameter
between the keyword static and the return type void
.
What does this type parameter
specify and how does it constrain the types of the elements of the arrays to which the first mergeSort
method can be applied?Write and execute JUnit tests (one method per test) to determine whether Weiss’s mergeSort
procedure correctly rearranges arrays.
isCorrectlyOrdered
that takes an array of AnyType
as its argument and determines whether its elements are correctly ordered.
isCorrectlyOrdered
method, check the ordering of every two contiguous elements of the sorted array. They should be ordered such that arr[i] < arr[i + 1]
.type parameter
section as shown above. This indicates that the elements of the array must be Comparable
together. Based on the description of the method, what should be the return type?isCorrectlyOrdered
method accepts an array object as its parameter. Remember that you can find useful methods in the Java documentation for the List interface.24, 13, 26, 1, 2, 27, 38, and 15
, in that order, so that at the end of the sorting process the elements of the array are in ascending numerical order.eight-cousins.txt.tokenized
and store them in a List
. (This file contains the full text of Louisa May Alcott’s novel Eight Cousins, with one word or punctuation mark on a separate line.)mergeSort
method will return it unchanged, without ever invoking the merge
method.The Switch
class that we used in the “Lab 5: References and Objects” lab does not implement the Comparable
interface, so we can’t use Weiss’s method to sort an array of Switch
objects directly.
Define a ComparableSwitch
class that extends Switch
, giving it a compareTo
method that ensures that every switch that is turned off is ordered ahead of any switch that is turned on.
Add JUnit methods to your tester class, test your code on a small array of 5 ComparableSwitch
objects, set randomly to “on” or “off” (find the appropriate method in the Random class). Print your array before and after the sort so that you know if your code is working properly. Then write and execute a test that creates an array of ten thousand ComparableSwitch objects, randomly turns them on/off, calls mergeSort to sort them, and then checks to confirm that all of the “off” switches precede all of the “on” switches in the sorted array.
You will submit your source files and the lab report (include the JUnit output) via Gradescope by the end of the week.
I strongly recommend that each student keep a copy of the lab. Therefore, don’t forget to share the files/folder with your lab partner!
Remember: Do not use magic numbers, write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). Please include comments or organize your code and report in a way that will help the grader to find the answer to the exercises or posted questions.
From Data Structures & Problem Solving Using Java by Mark Allen Weiss