125 lines
4.4 KiB
Java
125 lines
4.4 KiB
Java
package Sorting;
|
|
|
|
import java.util.Scanner;
|
|
import java.util.InputMismatchException;
|
|
/**
|
|
* 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, selection, insertion, quick, counting, radix, merge, ready;
|
|
System.out.print("Please enter file name: ");
|
|
filename = sc.nextLine();
|
|
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");
|
|
}
|
|
|
|
}
|
|
}
|
|
} |