Files
Tutoring-APCompSci/Sorting/Sorter.java

120 lines
3.9 KiB
Java

package Sorting;
import java.time.Duration;
import java.time.Instant;
import java.lang.Integer;
/**
* This is an abstract class, its subclasses will inherit its methods and instance
* fields. Any abstract methods will have to be implemented in the subclasses.
*/
abstract class Sorter {
protected String sortType;
protected Integer[] numbers;
protected String[] words;
protected long comparisonsUsed = 0;
protected long writesUsed=0;
protected boolean sortingNumbers;
public Sorter(String st, RandomNumberFileReader reader){
sortType=st;
//RandomNumberFileReader reader = new RandomNumberFileReader(filename);
numbers = new Integer[reader.getNumbers().size()];
reader.getNumbers().toArray(numbers);
sortingNumbers = true;
}
public Sorter(String sortType, RandomWordFileReader reader){
this.sortType = sortType;
words = new String[reader.getWords().size()];
reader.getWords().toArray(words);
sortingNumbers = false;
}
/**
* An abstract sorting method. Each subclass will have to implement its own
* version of this method.
* @return A SortResult object holding number of comparisons, number of swaps, and the sorted array
*/
abstract void sort();
/**
* A method to swap two elements of an array and increment the writesUsed counter.
* @param a the index of the first element
* @param b the index of the second element
*/
protected void swap(int a, int b){
int tmp = numbers[a];
numbers[a] = numbers[b];
numbers[b] = tmp;
writesUsed+=2;
}
protected void swap(String[] array, int indexA, int indexB){
String tmp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = tmp;
writesUsed+=2;
}
/**
* A method to compare two integers and increment the comparisonsUsed counter.
* @param a the first integer to be compared
* @param b the second integer to be compared
* @return a negative number if the first integer is smaller than the second, 0 if they are equal, and a positive number if the first integer is greater than the second.
*/
protected int compare(int a, int b){
comparisonsUsed++;
return Integer.compare(a,b);
}
protected int compare(String a, String b){
comparisonsUsed++;
return a.compareTo(b);
}
/**
* Writes a value into an array at a specific index and increments the writesUsed counter.
* @param array the array to be used
* @param index the index to be written to
* @param value the value to write
*/
protected void writeToArray(Integer[] array, int index, int value){
array[index]=value;
writesUsed++;
}
protected void writeToArray(String[] array, int index, String value){
array[index]=value;
writesUsed++;
}
/**
* Measures how long the sort() function takes to run
* @return returns a SortResult object with the name of the sorting algorithm used, the sorted array of numbers, the number of comparisons used to sort, and the number of writes to arrays used to sort
*/
public SortResult measuredSort(){
if(numbers!=null||words!=null){
Instant start = Instant.now();
this.sort();
Instant end = Instant.now();
Duration time = Duration.between(start,end);
SortResult output;
if(sortingNumbers){
output = new SortResult(sortType, numbers, comparisonsUsed, writesUsed, time);
}
else{
output = new SortResult(sortType, words, comparisonsUsed, writesUsed, time);
}
return output;
}
else{
System.out.println("Nothing to sort using "+sortType);
return null;
}
}
}