diff --git a/Sorting/WordSorter.java b/Sorting/WordSorter.java new file mode 100644 index 0000000..28e082b --- /dev/null +++ b/Sorting/WordSorter.java @@ -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; + } + } + + +} \ No newline at end of file