import java.util.Comparator; /* An instance of this class, which implements the ArraySorter interface, ** has methods by which to sort an array (or a segment thereof) using the ** classic Selection Sort algorithm. */ public class SelectionSorter implements ArraySorter { private Comparator comp; // intended to define a total ordering on type T /* Establishes this Selection Sorter as having the given comparator as its ** "resident" comparator. */ public SelectionSorter(Comparator c) { comp = (Comparator)c; } /* Rearranges the elements in a[] so as to be in ascending order ** according to the resident comparator. */ public void sort(T[] a) { sort(a, 0, a.length); } /* pre: 0<=low<=high<=a.length ** post: elements in a[low..high-1] are same as originally, but ** rearranged to be in ascending order (with respect to this.comp). */ public void sort(T[] a, int low, int high) { int locOfMin; for (int i=low; i != high; i=i+1) { locOfMin = findMin(a, i, high); swap(a,i,locOfMin); } } /* pre: 0<=low 0) { resultSoFar = j; } } return resultSoFar; } /* pre: 0 <= i,j < b.length * post: references in b[i] and b[j] have been swapped */ private void swap(T[] b, int i, int j) { T temp = b[i]; b[i] = b[j]; b[j] = temp; } }