import SortableIntSeq; /** * Test some of the methods given in SortableIntSeq. * * @author Samuel A. Rebelsky * @version 1.0 of September 1998 */ public class SortTester { public static void main(String[] args) { // Prepare to read input and generate output SimpleInput in = new SimpleInput(); SimpleOutput out = new SimpleOutput(); // Set up a sequence SortableIntSeq numbers = new SortableIntSeq(); // The intended length of the sequence int length; // Which algorithm should we run? See the messages for more // information on numbering of algorithms. int algorithm; // Should we print the sequence? 0 for no, 1 for yes int print; // A counter variable for loops int i; // Determine which algorithm to run. out.println("Select an algorithm: "); out.println(" 1: find the smallest element"); out.println(" 2: find the two smallest elements"); out.println(" 3: find the five smallest elements using the naive method"); out.println(" 4: find the five smallest elements using a better method"); // out.println(" 10: sort using selection sort"); out.println(" 11: sort using insertion sort"); out.println(" 12: sort using Quicksort"); out.print("Algorithm: "); algorithm = in.readInt(); // Determine the length of the sequence. Build that sequence. out.print("What length sequence do you want? "); length = in.readInt(); numbers.randomFill(length); // See if the user wants the original sequence out.print("View the original sequence? (0 for no, 1 for yes) "); print = in.readInt(); if (print == 1) { out.println("Original sequence: " + numbers.toString()); } // if the user wants to see the original sequence // Run the algorithm and print the results switch (algorithm) { case 1: // smallest element out.println("The smallest element is " + numbers.smallest()); break; case 2: // two smallest elements numbers.twoSmallest(); out.println("The smallest element is " + numbers.get(0)); out.println("The next smallest element is " + numbers.get(1)); break; case 3: // five smallest elements, naive method numbers.fiveSmallest(); for (i = 0; i < 5; ++i) { out.println("The " + i + "th smallest element is " + numbers.get(i)); } // for break; case 4: // five smallest elements, better method numbers.newFiveSmallest(); for (i = 0; i < 5; ++i) { out.println("The " + i + "th smallest element is " + numbers.get(i)); } // for break; /* Selection sort is unimplemented. case 10: // selection sort numbers.selectionSort(); break; */ case 11: // insertion sort numbers.insertionSort(); break; case 12: // quick sort numbers.quickSort(); break; default: out.println("Sorry, don't understand algorithm " + algorithm); break; } // switch // See if the user wants the revised sequence out.print("View the revised sequence? (0 for no, 1 for yes) "); print = in.readInt(); if (print == 1) { out.println("Revised sequence: " + numbers.toString()); } // if the user wants to see the original sequence } // main(String[]) } // class SortTester