initial commit

This commit is contained in:
2020-06-17 17:14:47 -05:00
parent b6808d49c3
commit 602e1ebdd1

54
Sorting/WordSorter.java Normal file
View File

@@ -0,0 +1,54 @@
package Sorting;
import java.time.Duration;
import java.time.Instant;
public abstract class WordSorter {
protected String sortType;
protected String[] words;
protected long comparisonsUsed = 0;
protected long writesUsed=0;
public WordSorter(String sortType, RandomWordFileReader reader){
this.sortType = sortType;
words = new String[reader.getWords().size()];
reader.getWords().toArray(words);
}
abstract void sort();
protected int compare(String a, String b){
comparisonsUsed++;
return a.compareTo(b);
}
protected void writeToArray(String[] array, int index, String value){
array[index]=value;
writesUsed++;
}
protected void swap(String[] array, int indexA, int indexB){
String tmp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = tmp;
writesUsed+=2;
}
public WordSortResult measuredSort(){
if(words.length!=0){
Instant start = Instant.now();
this.sort();
Instant end = Instant.now();
Duration time = Duration.between(start,end);
WordSortResult output = new WordSortResult(sortType, words, comparisonsUsed, writesUsed, time);
return output;
}
else{
System.out.println("Nothing to sort using "+sortType);
return null;
}
}
}