package Sorting; import java.time.Duration; import java.util.Locale; import java.text.NumberFormat; /** * This is a class to hold the efficiency results of a sorter. We will use this * because we can only have one return value from a method, but want to get more * than one piece of data from each sorter. * * We will write several constructors for this class, as not all sorting algorithms * will use all of the fields. For example, radix sort does not use comparisons. * Counting sort does not use swaps. * * Writing several constructors for difference situations is an example of * "polymorphism" -- the quality of object oriented design that lets a single * interface have multiple implementations. * * The methods that call the constructor of SortResult don't know it has lots of * them, they only know what data they have to give it. */ public class SortResult { private String sortType; private Integer [] sortedArray; private String[] sortedWordArray; private String comparisonsUsed; private String writesUsed; private String timeUsed; private boolean sortingNumbers; public SortResult(String st, Integer[] a, long c, long w, Duration t){ sortingNumbers = true; sortType = st; sortedArray = a; NumberFormat nf = NumberFormat.getInstance(Locale.US); if(c==0){ comparisonsUsed = "No"; } else{ comparisonsUsed = nf.format(c); } writesUsed = nf.format(w); timeUsed = nf.format(t.toMillis()); } public SortResult(String st, String[] a, long c, long w, Duration t){ sortingNumbers = false; sortType = st; sortedWordArray = a; NumberFormat nf = NumberFormat.getInstance(Locale.US); if(c==0){ comparisonsUsed = "No"; } else{ comparisonsUsed = nf.format(c); } writesUsed = nf.format(w); timeUsed = nf.format(t.toMillis()); } public SortResult(String st, Integer[] a, Duration t){ sortType = st; sortedArray = a; NumberFormat nf = NumberFormat.getInstance(Locale.US); timeUsed = nf.format(t.toMillis()); comparisonsUsed = "Unknown"; writesUsed = "Unknown"; } public String getSortType(){ return sortType; } public Integer[] getSortedNumbers(){ return sortedArray; } public String[] getSortedWords(){ return sortedWordArray; } public String getTimeUsed(){ return timeUsed; } public String getComparisonsUsed(){ return comparisonsUsed; } public String getWritesUsed(){ return writesUsed; } public String getSortCount(){ if(sortingNumbers){ NumberFormat nf = NumberFormat.getInstance(Locale.US); return nf.format(sortedArray.length); } else { NumberFormat nf = NumberFormat.getInstance(Locale.US); return nf.format(sortedWordArray.length); } } public String getMin(){ NumberFormat nf = NumberFormat.getInstance(Locale.US); return nf.format(sortedArray[0]); } public String getMax(){ NumberFormat nf = NumberFormat.getInstance(Locale.US); return nf.format(sortedArray[sortedArray.length-1]); } public String getFirst(){ return sortedWordArray[0]; } public String getLast(){ return sortedWordArray[sortedWordArray.length-1]; } }