added string array and boolean array for algorithm choices, to make adding new ones easier
This commit is contained in:
@@ -80,46 +80,38 @@ public class AlgorithmTester{
|
|||||||
private static void sortFile(){
|
private static void sortFile(){
|
||||||
String filename;
|
String filename;
|
||||||
int input;
|
int input;
|
||||||
boolean bubble=false, selection=false, insertion=false;
|
// boolean bubble=false, selection=false, insertion=false;
|
||||||
boolean quick=false, counting=false; //radix=false, merge=false;
|
//boolean quick=false, counting=false, radix=false, merge=false;
|
||||||
boolean ready=false;
|
boolean ready=false;
|
||||||
boolean print=false;
|
boolean print=false;
|
||||||
|
String[] sortingAlgoNames = {"bubble","selection","insertion","counting","quick"};
|
||||||
|
boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length];
|
||||||
System.out.print("\nPlease enter file name: ");
|
System.out.print("\nPlease enter file name: ");
|
||||||
filename = sc.next();
|
filename = sc.next();
|
||||||
while(!ready){
|
while(!ready){
|
||||||
try{
|
try{
|
||||||
System.out.println("\nEnter the number of an algorithm you wish to use.");
|
System.out.println("\nEnter the number of an algorithm you wish to use.");
|
||||||
System.out.println("Enter the number again to deselect an algorithm.");
|
System.out.println("Enter the number again to deselect an algorithm.");
|
||||||
if(bubble){
|
for(int i=0;i<sortingAlgoNames.length;i++){
|
||||||
System.out.print("*");
|
int num=i+1;
|
||||||
|
System.out.print(num+") "+sortingAlgoNames[i]);
|
||||||
|
if(sortingAlgoChoices[i]){
|
||||||
|
System.out.print(" *\n");
|
||||||
}
|
}
|
||||||
System.out.println("1) bubble sort");
|
else{
|
||||||
if(selection){
|
System.out.print("\n");
|
||||||
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");
|
|
||||||
System.out.println("Enter 0 to continue when all desired algorithms have been selected.");
|
System.out.println("Enter 0 to continue when all desired algorithms have been selected.");
|
||||||
input = sc.nextInt();
|
input = sc.nextInt();
|
||||||
switch(input){
|
if(input>0&&input<=sortingAlgoChoices.length){
|
||||||
case 1 -> bubble=!bubble;
|
sortingAlgoChoices[input-1]=!sortingAlgoChoices[input-1];
|
||||||
case 2 -> selection=!selection;
|
}
|
||||||
case 3 -> insertion=!insertion;
|
else if(input==0){
|
||||||
case 4 -> counting=!counting;
|
ready=true;
|
||||||
case 5 -> quick=!quick;
|
}
|
||||||
case 0 -> ready=true;
|
else{
|
||||||
default -> System.out.println("Invalid input");
|
System.out.println("Invalid input");
|
||||||
}
|
}
|
||||||
}catch(InputMismatchException ex){
|
}catch(InputMismatchException ex){
|
||||||
System.out.println("Invalid input");
|
System.out.println("Invalid input");
|
||||||
@@ -138,7 +130,13 @@ public class AlgorithmTester{
|
|||||||
}catch(InputMismatchException ex){
|
}catch(InputMismatchException ex){
|
||||||
System.out.println("Invalid input, defaulting to no");
|
System.out.println("Invalid input, defaulting to no");
|
||||||
}
|
}
|
||||||
|
boolean bubble=sortingAlgoChoices[0];
|
||||||
|
boolean selection=sortingAlgoChoices[1];
|
||||||
|
boolean insertion=sortingAlgoChoices[2];
|
||||||
|
boolean counting=sortingAlgoChoices[3];
|
||||||
|
boolean quick=sortingAlgoChoices[4];
|
||||||
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
||||||
|
|
||||||
if(bubble){
|
if(bubble){
|
||||||
BubbleSorter bubSorter = new BubbleSorter(filename);
|
BubbleSorter bubSorter = new BubbleSorter(filename);
|
||||||
results.add(bubSorter.measuredSort());
|
results.add(bubSorter.measuredSort());
|
||||||
|
|||||||
Reference in New Issue
Block a user