/* * Purpose: This class tests and reports the efficiency of sorting algorithms * Authors: Ben Hample, Greg Krywicki, Robert Russell * Date: 12/18/07 */ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; public class Driver { //the chosen size of the array to test the sorting algorithms private static int ARRAY_SIZE; /* * Main Method */ public static void main(String[] args) { //size of the randomly generated Employee array from the user int set = Integer.parseInt(args[0]); ARRAY_SIZE = set; Driver d = new Driver(); } /* * Purpose: Constructor * Pre: The implementations of the sorting algorithms work correctly * Post All implementations have been tested * In: None * Out: None */ public Driver() { //We store the randomly generated array of Employees //four times because we want each sorting algoritm to //sort the exact same array. This ensures that our times //for how long each sorting algorithm took is even more accurate //since we are dealing with the same data for each sorting algoritm Employee[] theArray = generateEmployees(); Employee[] insertionArray = theArray.clone(); Employee[] quickArray = theArray.clone(); Employee[] quickRecArray = theArray.clone(); Employee[] radixArray = theArray.clone(); theArray = null; //test each sorting algorithm with the randomly generated Employee array insertionSortTest(insertionArray); quickSortTest(quickArray); quickSortRecTest(quickRecArray); radixSortTest(radixArray); } /* * Purpose: Insertion sort is tested and the time it took * to sort the array in milliseconds is recorded * Pre: The implementation of insertion sort is correct * Post: The pre-sorted array is sent to a file, the array is sorted, * the sorted array sent to a file, and the time to took to sort is * recorded in millseconds * In: None * Out: None */ public void insertionSortTest(Employee[] theArray) { //populate an array with random Employees and //send the names and ages of these Employees to file Employee[] insertionArray = theArray; printToFile(insertionArray, "Pre-Insertion"); long start = System.currentTimeMillis(); InsertionSort.insertionSort(insertionArray); long end = System.currentTimeMillis(); long duration = end - start; System.out.println("Insertion sort took " + duration + " milliseconds."); printToFile(insertionArray, "Post-Insertion"); } /* * Purpose: Quick sort is tested and the time it took * to sort the array in milliseconds is recorded * Pre: The implementation of quick sort is correct * Post: The pre-sorted array is sent to a file, the array is sorted, * the sorted array sent to a file, and the time to took to sort is * recorded in millseconds * In: None * Out: None */ public void quickSortTest(Employee[] theArray) { //populate an array with random Employees and //send the names and ages of these Employees to file Employee[] quickArray = theArray; printToFile(quickArray, "Pre-Quick"); //calculate the amount of time it took to sort the array //and send it to output long start = System.currentTimeMillis(); QuickSort.quickSort(quickArray, 0, quickArray.length - 1); long end = System.currentTimeMillis(); long duration = end - start; System.out.println("Quick sort took " + duration + " milliseconds."); //send the names and ages of the Employees after the array //is sorted to file printToFile(quickArray, "Post-Quick"); } /* * Purpose: Quick sort (recursive) is tested and the time * it took to sort the array in milliseconds is recorded * Pre: The implementation of quick sort (recursive) is correct * Post: The pre-sorted array is sent to a file, the array is sorted, * the sorted array sent to a file, and the time to took to sort is * recorded in millseconds * In: None * Out: None */ public void quickSortRecTest(Employee[] theArray) { //populate an array with random Employees and //send the names and ages of these Employees to file Employee[] quickRecArray = theArray; printToFile(quickRecArray, "Pre-QuickRec"); //calculate the amount of time it took to sort the array //and send it to output long start = System.currentTimeMillis(); QuickSortRec quickSortRec = new QuickSortRec(quickRecArray); long end = System.currentTimeMillis(); long duration = end - start; System.out.println("Quick sort with recursion took " + duration + " milliseconds."); //send the names and ages of the Employees after the array //is sorted to file printToFile(quickRecArray, "Post-QuickRec"); } /* * Purpose: Radix sort is tested and the time it took * to sort the array in milliseconds is recorded * Pre: The implementation of radix sort is correct * Post: The pre-sorted array is sent to a file, the array is sorted, * the sorted array sent to a file, and the time to took to sort is * recorded in millseconds * In: None * Out: None */ public void radixSortTest(Employee[] theArray) { //populate an array with random Employees and //send the names and ages of these Employees to file Employee[] radixArray = theArray; printToFile(radixArray, "Pre-Radix"); //calculate the amount of time it took to sort the array //and send it to output long start = System.currentTimeMillis(); RadixSort.radixSort(radixArray); long end = System.currentTimeMillis(); long duration = end - start; System.out.println("Radix sort took " + duration + " milliseconds."); //send the names and ages of the Employees after the array //is sorted to file printToFile(radixArray, "Post-Radix"); } /* * Purpose: Generate the array with Employees that have names * that start with a random upper case character followed by 1-9 * random lower case characters and a random a random age * Pre: None * Post: None * In: None * Out: The array of Employees */ private Employee[] generateEmployees() { //initialize the array of Emplyoees and an instance of Random Employee[] employeeArray = new Employee[ARRAY_SIZE]; Random rnd = new Random(); //go through each array position and generate a random Employee //and place it into the array at the current position for(int i = 0; i < ARRAY_SIZE; i++) { //generate a random age (1-100) //and add one so the age is never zero int age = rnd.nextInt(100) + 1; //generate a random name length (1-10) //and add one so the length is never zero int nameLength = rnd.nextInt(9) + 1; String name = ""; //create characters for however long the name //is meant to be for this Employee for(int k = 0; k < nameLength; k++) { int charVal = 0; //we need a number from 65 through 90 because //the first character (k ==0) needs to be capital letter //and the unicode conversion for 65 to 90 is upper case letters if(k == 0) { //it is 91 because the nextInt method is exclusive charVal = rnd.nextInt(91); //nextInt could generate a number lower than 96, which is not //uppercase, so keep trying until it suceeds while(charVal < 65) { charVal = rnd.nextInt(91); } } else //it isnt the first character so we want lower case { //it is 123 because the nextInt method is exclusive charVal = rnd.nextInt(123); //nextInt could generate a number lower than 97, which is not //lower case, so keep trying until it suceeds while(charVal < 97) { charVal = rnd.nextInt(123); } } //take the random value and cast it as a char to obtain //the letter it represents and then concatenate it onto the name char ch = (char) charVal; name += ch; } //once the name is complete, create an employe with this //random name and age that was created and store it into the array Employee emp = new Employee(name, age); employeeArray[i] = emp; } return employeeArray; } /* * Purpose: Take the Employees of a pre-sorted or sorted array * and print the Employee details (meaning name and age) to a file * Pre: The array is valid * Post: Each Employee name and age is printed in order to file * In: The array of Employees and the name of the file * Out: None */ private void printToFile(Employee[] array, String name) { PrintWriter out = null; //by defintion, try { out = new PrintWriter(new FileWriter(name + ".txt"), true); } catch (IOException ex) { ex.printStackTrace(); } for(int i = 0; i < array.length; i++) { out.println(array[i].toString()); } } }