/** * Takes a given input file and averages the number of trials, then writes to outputFile. * runAlgorithms() can run multiple times, this is to get a csv file with * * @param br * @param numTrials * @param inputName * @param outputName * @throws IOException */ private static void averageIntCSVResults( BufferedReader br, int numTrials, String inputName, String outputName) throws IOException { String[] curLineSplit = br.readLine().split(","); String curName = curLineSplit[0]; int curSize = Integer.parseInt(curLineSplit[3]); double avg = Double.parseDouble(curLineSplit[1]); String curLine = ""; long numComparisons = Long.parseLong(curLineSplit[2]); while ((curLine = br.readLine()) != null) { curLineSplit = curLine.split(","); if (avg == 0 && numComparisons == 0) { curSize = Integer.parseInt(curLineSplit[3]); curName = curLineSplit[0]; numComparisons = Long.parseLong(curLineSplit[2]); } if (curLineSplit[0].equals(curName) && Integer.parseInt(curLineSplit[3]) == curSize) { avg += Double.parseDouble(curLineSplit[1]); // numComparisons += Long.parseLong(curLineSplit[2]); } else { avg /= numTrials; // numComparisons /= numTrials; String output = curName + "," + avg + "," + numComparisons; sortWrite(outputName, output, curSize); avg = 0; numComparisons = 0; } } String output = curName + "," + avg + "," + numComparisons; sortWrite(outputName, output, curSize); }
/** * Runs algorithm on various list sizes with various algorithms. TODO: Make use of input * parameters. * * @param args */ public static void runSortingAlgorithms(String[] args) { // Sorter[] s = {new BubbleSort(args)}; // Sorter[] s = {new ExchangeSort(args), // new InsertionSort(args), new MergeSort(args),new QuickSort(args), new SelectionSort(args)}; Sorter[] s = {new QuickSort(args), new MergeSort(args)}; String outputName = "algtimings11.csv"; int[] listSizes = new int[] { 100, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 25000, 50000, 75000, 100000 }; s[0].hybrid(new SelectionSort(), 7); s[1].hybrid(new SelectionSort(), 7); Integer[][] lists = new Integer[listSizes.length][listSizes.length]; for (int i = 0; i < listSizes.length; i++) { lists[i] = getIntArray(listSizes[i] + ".csv"); } int numTests = 6; for (Sorter s1 : s) { for (int j = 0; j < lists.length; j++) { // Lists for (int k = 0; k < numTests; k++) { // number of interations s1.sort(lists[j].clone()); DataFeed.sortWrite(outputName, s1.getData(), lists[j].length); } } } }
/** * Writes a line that is given by sorter.getData() * * @param line * @param n */ public static void sortWrite(String path, String line, int n) { Path p = Paths.get(path); try { List<String> lines = Files.readAllLines(p); if (lines.isEmpty()) { lines.add("Name,Time,Comparisons,Size"); } lines.add(line + "," + n); Files.write(Paths.get(p.toString()), lines); } catch (IOException e) { if (e.getCause() == new NoSuchFieldException().getCause()) { try { Files.createFile(p); sortWrite(path, line, n); } catch (IOException ex) { java.util.logging.Logger.getLogger(SortLauncher.class.getName()) .log(Level.SEVERE, null, ex); } } } }