package Sorting; import java.util.Scanner; import java.util.ArrayList; import java.util.InputMismatchException; //import java.time.temporal.ChronoUnit; /** * This class contains the main method and presents an interface for the user to * compare sorting algorithm efficiencies. The user will be able to generate files * of random numbers, and to select a set of algorithms to test on a file of random * numbers. */ public class AlgorithmTester{ final static Scanner sc = new Scanner(System.in); static int input; static boolean quit = false; public static void main(String[] args) { while(!quit){ System.out.println("\nSORTING ALGORITHM TESTER"); System.out.println("Please select an option:"); System.out.println("1) Make a file of random numbers"); System.out.println("2) Sort a file of random numbers"); System.out.println("0) Exit"); try{ input = sc.nextInt(); //Using the new switch syntax introduced in JDK 13 //this is a switch expression (lambda expression) rather than a switch statement switch(input){ case 1 -> makeFile(); case 2 -> sortFile(); case 0 -> quit=true; default -> System.out.println("Invalid input"); } /* non-lambda expresstion version looks like this switch(input){ case 1: makeFile(); break; case 2: sortFiler(); break; case 0: quit=true; break; default: System.out.println("Invalid input"); break; } */ } catch(InputMismatchException ex){ System.out.println("Invalid input exception"); sc.next(); } } sc.close(); } private static void makeFile(){ String filename; int count; int min; int max; try{ System.out.print("\nPlease enter a file name: "); filename = sc.next(); System.out.println("How many random numbers?"); count = sc.nextInt(); System.out.println("Minimum value?"); min = sc.nextInt(); System.out.println("Maximum value?"); max = sc.nextInt(); var randomMaker = new RandomNumberFileMaker(filename, count, min, max); randomMaker.writeFile(); } catch(InputMismatchException ex){ System.out.println("Invalid input."); sc.next(); } } private static void sortFile(){ String filename; int input; boolean ready=false; boolean print=false; String[] sortingAlgoNames = {"bubble sort","selection sort","insertion sort","counting sort","quick sort","quick sort (3-way partition)","merge sort"}; boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length]; System.out.print("\nPlease enter file name: "); filename = sc.next(); while(!ready){ try{ System.out.println("\nEnter the number of an algorithm you wish to use."); System.out.println("Enter the number again to deselect an algorithm."); for(int i=0;i "); } System.out.print(sortingAlgoNames[i]+"\n"); } System.out.println("Enter 0 to continue when all desired algorithms have been selected."); input = sc.nextInt(); if(input>0&&input<=sortingAlgoChoices.length){ sortingAlgoChoices[input-1]=!sortingAlgoChoices[input-1]; } else if(input==0){ ready=true; } else{ System.out.println("Invalid input"); } }catch(InputMismatchException ex){ System.out.println("Invalid input"); sc.next(); } } try{ System.out.println("\nPrint sorted list?"); System.out.println("1) Yes"); System.out.println("2) No"); input = sc.nextInt(); switch(input){ case 1 -> print=true; case 2 -> print=false; default -> System.out.println("Invalid input, defaulting to no"); } }catch(InputMismatchException ex){ System.out.println("Invalid input, defaulting to no"); sc.next(); } boolean bubble=sortingAlgoChoices[0]; boolean selection=sortingAlgoChoices[1]; boolean insertion=sortingAlgoChoices[2]; boolean counting=sortingAlgoChoices[3]; boolean quick=sortingAlgoChoices[4]; boolean quickTWP=sortingAlgoChoices[5]; boolean merge=sortingAlgoChoices[6]; ArrayList results = new ArrayList(); RandomNumberFileReader reader = new RandomNumberFileReader(filename); if(bubble){ BubbleSorter bubSorter = new BubbleSorter(reader); results.add(bubSorter.measuredSort()); } if(selection){ SelectionSorter selSorter = new SelectionSorter(reader); results.add(selSorter.measuredSort()); } if(insertion){ InsertionSorter inSorter = new InsertionSorter(reader); results.add(inSorter.measuredSort()); } if(counting){ CountingSorter countSorter = new CountingSorter(reader); results.add(countSorter.measuredSort()); } if(quick){ QuickSorter qSorter = new QuickSorter(reader); results.add(qSorter.measuredSort()); } if(quickTWP){ QuickSorterThreeWay qTWPSorter = new QuickSorterThreeWay(reader); results.add(qTWPSorter.measuredSort()); } if(merge){ MergeSorter mSorter = new MergeSorter(reader); results.add(mSorter.measuredSort()); } for(SortResult e: results){ if(print){ for(int i: e.getSortedArray()){ System.out.print(i+" "); } System.out.print("\n"); } System.out.println("\n"+e.getSortType()+" of "+e.getSortCount()+" numbers with range "+e.getMin()+" to "+e.getMax()+" took:"); System.out.println(e.getComparisonsUsed()+" comparisons"); System.out.println(e.getWritesUsed()+" write operations"); System.out.println(e.getTimeUsed()+" milliseconds"); } } }