0

I am currently working on a school project that tests the run times of different sorting methods. We have been provided a class file containing the sort methods (1 - 5). In my driver code, I need to be able to call each of these methods sequentially inside the loop in the sortTester() method. An individual method can be called like: sortItOut.sort1(randomIntsCopy);. My code is as follows, thank you!

import java.io.*;
import java.security.SecureRandom;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;

public class Driver {
    public static void main(String[] args) throws IOException {
        try {
            System.out.println();
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.printf("%nError: When executing this program, please " +
            "enter the command followed by an integer that will determine " +
            "size of the array of random integers that is created.%n");
            System.exit(0); // Terminates program
        }        
        // Creates an integer, 'n', from command line argument
        Integer size = Integer.valueOf(args[0]);
        // Creates an array of size 'n'
        int[] randomInts = new int[size];

        // Creates instance of SecureRandom class
        SecureRandom secureRandom = new SecureRandom();
        // Populates the array with random integers
        for (int i = 0; i < randomInts.length; i++) {
            randomInts[i] = secureRandom.nextInt();
        }

        int[] results = new int[5];

        sortTester(randomInts, results);
        displayResults(size, results); // Outputs table of execution times
    }

    public static void sortTester(int[] randomInts, int[] results) {
        for (int i = 0; i < 5; i++) {
            System.out.printf("Sorting array with mystery sort %d...", i+1);
            // New copy of original randomInts array
            int[] randomIntsCopy = randomInts.clone();
            Instant strtTime = Instant.now(); // Starts clock

            sortItOut.sort1(randomIntsCopy); // Sorts the array

            Instant endTime = Instant.now(); // Ends clock
            // Calculates the execution time for each iteration
            long time = (Duration.between(strtTime, endTime).toMillis());
            int timeAsInt = (int) time; // Converts long -> int
            results[i] = timeAsInt; // Populates array with each duration
            System.out.printf("   Done!%n");
        }
    }

    public static void displayResults(int size, int[] results) {
        System.out.printf("%n---------------------------%n");
        System.out.printf("| %-9s |      %6d |%n", "    N    ", size);
        System.out.printf("---------------------------%n");

        for (int i = 0; i < 5; i++) {
            System.out.printf("| %-9s | %8d ms |%n", "sort" + (i+1) + "Time", 
                results[i]);
            System.out.printf("---------------------------%n");
        }
        System.out.println();
    }
}
Fuji
  • 41
  • 6

0 Answers0