Add code to sort word file

This commit is contained in:
2020-06-17 20:50:09 -05:00
parent 96a7f1f00e
commit d7c8a8e8f2

View File

@@ -192,6 +192,82 @@ public class AlgorithmTester{
}
private static void sortWordFile(){
//String filename;
String filename;
int input;
boolean ready=false;
boolean print=false;
String[] sortingAlgoNames = {"selection sort", "insertion 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];
ArrayList<WordSortResult> results = new ArrayList<WordSortResult>();
if(selection){
//WordSelectionSorter selSorter = new NumberSelectionSorter(reader);
//results.add(selSorter.measuredSort());
}
if(insertion){
WordInsertionSorter inSorter = new WordInsertionSorter(reader);
results.add(inSorter.measuredSort());
}
for(WordSortResult e: results){
if(e!=null){
if(print){
for(String i: e.getSortedArray()){
System.out.print(i+" ");
}
System.out.print("\n");
}
System.out.println("\n"+e.getSortType()+" of "+e.getSortCount()+" words from "+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");
}
}
}
}