280 lines
11 KiB
Java
280 lines
11 KiB
Java
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("3) Sort a file of words");
|
|
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 3 -> sortWordFile();
|
|
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","insertion sort (linked list)","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();
|
|
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
|
|
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<sortingAlgoNames.length;i++){
|
|
int num=i+1;
|
|
System.out.print(num+") ");
|
|
if(sortingAlgoChoices[i]){
|
|
System.out.print("> ");
|
|
}
|
|
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 insertionll=sortingAlgoChoices[3];
|
|
boolean counting=sortingAlgoChoices[4];
|
|
boolean quick=sortingAlgoChoices[5];
|
|
boolean quickTWP=sortingAlgoChoices[6];
|
|
boolean merge=sortingAlgoChoices[7];
|
|
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
|
if(bubble){
|
|
NumberBubbleSorter bubSorter = new NumberBubbleSorter(reader);
|
|
results.add(bubSorter.measuredSort());
|
|
}
|
|
if(selection){
|
|
NumberSelectionSorter selSorter = new NumberSelectionSorter(reader);
|
|
results.add(selSorter.measuredSort());
|
|
}
|
|
if(insertion){
|
|
NumberInsertionSorter inSorter = new NumberInsertionSorter(reader);
|
|
results.add(inSorter.measuredSort());
|
|
}
|
|
if(insertionll){
|
|
NumberInsertionSorterLinkedList inSorterLL = new NumberInsertionSorterLinkedList(reader);
|
|
results.add(inSorterLL.measuredSort());
|
|
}
|
|
if(counting){
|
|
NumberCountingSorter countSorter = new NumberCountingSorter(reader);
|
|
results.add(countSorter.measuredSort());
|
|
}
|
|
if(quick){
|
|
NumberQuickSorter qSorter = new NumberQuickSorter(reader);
|
|
results.add(qSorter.measuredSort());
|
|
}
|
|
if(quickTWP){
|
|
NumberQuickSorterThreeWay qTWPSorter = new NumberQuickSorterThreeWay(reader);
|
|
results.add(qTWPSorter.measuredSort());
|
|
}
|
|
if(merge){
|
|
NumberMergeSorter mSorter = new NumberMergeSorter(reader);
|
|
results.add(mSorter.measuredSort());
|
|
}
|
|
for(SortResult e: results){
|
|
if(e!=null){
|
|
if(print){
|
|
for(int i: e.getSortedNumbers()){
|
|
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");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private static void sortWordFile(){
|
|
String filename;
|
|
int input;
|
|
boolean ready=false;
|
|
boolean print=false;
|
|
String[] sortingAlgoNames = {"selection sort", "insertion sort", "quick sort"};
|
|
boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length];
|
|
System.out.print("\nPlease enter file name: ");
|
|
filename = sc.next();
|
|
RandomWordFileReader reader = new RandomWordFileReader(filename);
|
|
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<sortingAlgoNames.length;i++){
|
|
int num=i+1;
|
|
System.out.print(num+") ");
|
|
if(sortingAlgoChoices[i]){
|
|
System.out.print("> ");
|
|
}
|
|
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 selection = sortingAlgoChoices[0];
|
|
boolean insertion = sortingAlgoChoices[1];
|
|
boolean quick = sortingAlgoChoices[2];
|
|
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
|
if(selection){
|
|
WordSelectionSorter selSorter = new WordSelectionSorter(reader);
|
|
results.add(selSorter.measuredSort());
|
|
}
|
|
if(insertion){
|
|
WordInsertionSorter inSorter = new WordInsertionSorter(reader);
|
|
results.add(inSorter.measuredSort());
|
|
}
|
|
if(quick){
|
|
//put wordquicksorter here
|
|
Sorter qSorter = new WordQuickSorter(reader);
|
|
results.add(qSorter.measuredSort());
|
|
}
|
|
for(SortResult e: results){
|
|
if(e!=null){
|
|
if(print){
|
|
for(String i: e.getSortedWords()){
|
|
System.out.print(i+" ");
|
|
}
|
|
System.out.print("\n");
|
|
}
|
|
System.out.println("\n"+e.getSortType()+" of "+e.getSortCount()+" words from \""+e.getFirst()+"\" to \""+e.getLast()+"\" took:");
|
|
System.out.println(e.getComparisonsUsed()+" comparisons");
|
|
System.out.println(e.getWritesUsed()+" write operations");
|
|
System.out.println(e.getTimeUsed()+" milliseconds");
|
|
}
|
|
}
|
|
|
|
}
|
|
} |