### CS171, Homework 5: adapt the QuickSort algorithm to use an interface

• Preparation

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs171/hw6 cp /home/cs171000/Handouts/hw6/*.java ~/cs171/hw6 ```

• Part 1: Adapt the Quick sort algorithm to sort an array of hw6Comparable objects

• You are given the following interface hw6Comparable (it's one of the files you copied from the handout directory):

 ``` public interface hw6Comparable { int compareTo( hw6Comparable x ); } ```

A class that implements this interface must defined a compareTo( hw6Comparable x ) method that operatess as follows:

• The compareTo(x) method must return:

 a negative value is this object is less than the input object x a posiive value is this object is greater than the input object x the value 0 otherwise

• You are also given the Quick Sort algorithm in the file QuickSort.java that sort an array of double:

 ``` public static void sort( double[] a, int iLeft, int iRight ) { int i, j, k; if ( iRight - iLeft <= 1 ) { // Array of 1 element is sorted.... return; } k = partition( a, iLeft, iRight ); // k = position of pivot // after partitioning /* ================================================= Sort each half ================================================= */ sort(a, iLeft, k); sort(a, k+1, iRight); /* ================================================= Concatenate the pieces back is NOT necessary ! ================================================= */ } ```

This is the same program that I have discussed in class (I have removed the print statements that print the input arrays).

• Part 1 of project:

 Change (i.e., edit) the QuickSort algorithm in the file QuickSort.java so that the method sort() will sort an array of hw6Comparable objects

• Test program for part 1:

• You can use the test program testDouble.java to test part 1:

 ``` public static void main( String[] args ) { hw6Double[] x = new hw6Double[N]; Random r = new Random(12345); for ( int i = 0; i < N; i++ ) x[i] = new hw6Double( (r.nextInt(80) + 10) / 10.0 ) ; System.out.println("Before sort: " + Arrays.toString(x) + "\n" ); QuickSort.sort( x, 0, x.length ); // Quick sort System.out.println("\nAfter sort: " + Arrays.toString(x) ); } ```

The testDouble.java test program will sort an array of hw6Double objects

Furthermore, the hw6Double class implements the hw6Comparable interface:

 ``` public class hw6Double implements hw6Comparable { public double value; public hw6Double(double x) { value = x; } public int compareTo( hw6Comparable a ) { hw6Double help = (hw6Double) a; if ( this.value < help.value ) return -1; else if ( this.value > help.value ) return 1; else return 0; } } ```

So that if your part 1 is correct, the test program will be able to sort an array of hw6Comparable objects

• How to test part 1:

 ``` Compile with this command: javac testDouble.java Run with this command: java testDouble ```

Note:

 Do not change the files testDouble.java and hw3Double.java You must change the file QuickSort.java so that the sort() and partition() methods will work using hw6Comparable as input type

The correct output is:

 ```Before sort: [2.1, 5.0, 1.1, 3.8, 6.5, 1.4, 2.5, 1.2, 7.1, 3.9, 8.7, 7.2, 4.0, 1.6, 6.2, 7.4, 7.7, 7.3, 6.2, 6.5, 5.1, 4.1, 8.2, 4.6, 3.8, 7.1, 7.5, 3.3, 4.1, 8.9, 5.9, 4.7, 3.1, 3.5, 3.3, 8.5, 4.2, 8.1, 6.0, 3.8, 4.6, 3.2, 2.9, 4.8, 4.8, 7.0, 6.2, 6.1, 8.0, 1.8] After sort: [1.1, 1.2, 1.4, 1.6, 1.8, 2.1, 2.5, 2.9, 3.1, 3.2, 3.3, 3.3, 3.5, 3.8, 3.8, 3.8, 3.9, 4.0, 4.1, 4.1, 4.2, 4.6, 4.6, 4.7, 4.8, 4.8, 5.0, 5.1, 5.9, 6.0, 6.1, 6.2, 6.2, 6.2, 6.5, 6.5, 7.0, 7.1, 7.1, 7.2, 7.3, 7.4, 7.5, 7.7, 8.0, 8.1, 8.2, 8.5, 8.7, 8.9] ```

• Part 2: using the Quick Sort algorithm to sort triangle objects

• In part 2, you are given a class that represent triangle obejcts:

 ``` public class hw6Triangle { public int base; public int height; public hw6Triangle(int b, int h) { base = b; height = h; } } ```

• In part 2, you will use the Quick Sort program that you have made in part 1 to sort an array of Triangle objects using the testTriangle test program:

 ``` public static void main( String[] args ) { hw6Triangle[] x = new hw6Triangle[N]; Random r = new Random(12345); for ( int i = 0; i < N; i++ ) x[i] = new hw6Triangle( r.nextInt(98) + 1, r.nextInt(98) + 1 ) ; System.out.println("Before sort: " + Arrays.toString(x) + "\n" ); QuickSort.sort( x, 0, x.length ); // Quick sort System.out.println("\nAfter sort: " + Arrays.toString(x) ); } ```

• Part 2:

 Change (i.e., edit) the hw3Triangle class in the file hw3Triangle.java so that the method sort() (in the file QuickSort.java) that you have made in part 1 will sort an array of hw3Triangle objects The triangle objects are to be sorted based on area If two different triangles have the same area, they are then ordered based on their height If two triangles have the same area and height, then they are equal (and compareTo() must return the value 0)

Note:

 Do not change the files testTriangle.java and QuickSort.java You must change the file hw3Triangle.java so that the sort() and partition() methods will work with hw3Triangle as input type

• How to test part 2:

 ``` Compile with this command: javac testTriangle.java Run with this command: java testTriangle ```

The correct output is:

 ```Before sort: [(6,3), (96,77), (80,47), (66,33), (14,30), (16,51), (29,31), (29,11), (8,16), (71,96), (76,58), (35,51), (83,72), (38,6), (68,46), (44,80), (28,64), (60,84), (71,66), (91,83), (29,37), (34,47), (31,29), (53,82), (77,57), (59,25), (13,79), (41,34), (15,62), (29,70), (89,96), (94,21), (98,70), (5,84), (97,72), (15,24), (85,15), (79,59), (98,33), (13,96), (68,51), (69,57), (96,17), (56,34), (95,85), (20,42), (44,48), (10,34), (68,39), (45,14)] After sort: [(6,3), (8,16), (38,6), (29,11), (10,34), (15,24), (14,30), (5,84), (45,14), (16,51), (20,42), (31,29), (29,31), (15,62), (13,79), (29,37), (13,96), (85,15), (41,34), (59,25), (34,47), (96,17), (35,51), (28,64), (56,34), (94,21), (29,70), (44,48), (66,33), (68,39), (68,46), (98,33), (68,51), (44,80), (80,47), (69,57), (53,82), (77,57), (76,58), (79,59), (71,66), (60,84), (83,72), (71,96), (98,70), (97,72), (96,77), (91,83), (95,85), (89,96)] ```

• Turnin

• Use the following command (from inside your cs171/hw6 project directory) to turn in your program:

 ``` cd ~/cs171/hw6 /home/cs171000/turnin QuickSort.java hw6 /home/cs171000/turnin hw3Triangle.java hw6a ```