158 lines
5.7 KiB
Java
158 lines
5.7 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) {
|
|
System.out.println("SORTING ALGORITHM TESTER\n");
|
|
while(!quit){
|
|
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");
|
|
}
|
|
}
|
|
sc.close();
|
|
}
|
|
|
|
private static void makeFile(){
|
|
String filename;
|
|
int count;
|
|
int min;
|
|
int max;
|
|
try{
|
|
System.out.print("Please 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");
|
|
}
|
|
}
|
|
|
|
private static void sortFile(){
|
|
String filename;
|
|
int input;
|
|
boolean bubble=false, selection=false, insertion=false;
|
|
boolean quick=false, counting=false; //radix=false, merge=false;
|
|
boolean ready=false;
|
|
System.out.print("Please enter file name: ");
|
|
filename = sc.next();
|
|
while(!ready){
|
|
try{
|
|
System.out.println("Enter the number of the algorithms you wish to use.");
|
|
System.out.println("Enter 0 to start sorting.");
|
|
if(bubble){
|
|
System.out.print("*");
|
|
}
|
|
System.out.println("1) bubble sort");
|
|
if(selection){
|
|
System.out.print("*");
|
|
}
|
|
System.out.println("2) selection sort");
|
|
if(insertion){
|
|
System.out.print("*");
|
|
}
|
|
System.out.println("3) insertion sort");
|
|
if(counting){
|
|
System.out.print("*");
|
|
}
|
|
System.out.println("4) counting sort");
|
|
if(quick){
|
|
System.out.print("*");
|
|
}
|
|
System.out.println("5) quick sort");
|
|
input = sc.nextInt();
|
|
switch(input){
|
|
case 1 -> bubble=!bubble;
|
|
case 2 -> selection=!selection;
|
|
case 3 -> insertion=!insertion;
|
|
case 4 -> counting=!counting;
|
|
case 5 -> quick=!quick;
|
|
case 0 -> ready=true;
|
|
default -> System.out.println("Invalid input");
|
|
}
|
|
}catch(InputMismatchException ex){
|
|
System.out.println("Invalid input");
|
|
}
|
|
}
|
|
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
|
if(bubble){
|
|
BubbleSorter bubSorter = new BubbleSorter(filename);
|
|
results.add(bubSorter.measuredSort());
|
|
}
|
|
if(selection){
|
|
SelectionSorter selSorter = new SelectionSorter(filename);
|
|
results.add(selSorter.measuredSort());
|
|
}
|
|
if(insertion){
|
|
InsertionSorter inSorter = new InsertionSorter(filename);
|
|
results.add(inSorter.measuredSort());
|
|
}
|
|
if(counting){
|
|
CountingSorter countSorter = new CountingSorter(filename);
|
|
results.add(countSorter.measuredSort());
|
|
}
|
|
if(quick){
|
|
QuickSorter qSorter = new QuickSorter(filename);
|
|
results.add(qSorter.measuredSort());
|
|
}
|
|
for(SortResult e: results){
|
|
//for(int i: e.getSortedArray()){
|
|
// System.out.print(i+" ");
|
|
//}
|
|
System.out.println("");
|
|
System.out.println(e.getSortType()+" sort took:");
|
|
System.out.println(e.getComparisonsUsed()+" comparisons");
|
|
System.out.println(e.getTimeUsed().toMillis()+" milliseconds\n");
|
|
}
|
|
|
|
}
|
|
} |