package Sorting; import java.time.Duration; import java.util.Locale; import java.text.NumberFormat; /** * This is a class to hold the efficiencty 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 WordSortResult { private String sortType; private String[] sortedArray; private String comparisonsUsed; private String writesUsed; private String timeUsed; public WordSortResult(String st, String[] a, long c, long w, Duration t){ 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 String getSortType(){ return sortType; } public String[] getSortedArray(){ return sortedArray; } public String getTimeUsed(){ return timeUsed; } public String getComparisonsUsed(){ return comparisonsUsed; } public String getWritesUsed(){ return writesUsed; } public String getSortCount(){ NumberFormat nf = NumberFormat.getInstance(Locale.US); return nf.format(sortedArray.length); } public String getMin(){ return sortedArray[0]; } public String getMax(){ return sortedArray[sortedArray.length-1]; } }