package sorting; import java.util.Comparator; import java.util.Vector; /** * Kyle's implementation of selection sort. * * @author Kyle Gonnerman * @author Samuel A. Rebelsky */ public class KyleSS implements Sorter { public void sort(Vector vec, Comparator c) { // For each position in the vector for (int i = vec.size()-1; i > 0; i = i-1) { // The largest thing found so far? Object largestSoFar = vec.get(i); // The position of the largest thing int indexOfLSF = i; // Look at each addition element for (int n = 0; n < i; n++) { int results = c.compare(largestSoFar, vec.get(n)); // If the n+1st value is larger than the largest so far if (results < 1) { // Update our notion of largest value largestSoFar = vec.get(n); // Update the position of the largest value indexOfLSF = n; } } // The thing at position i Object placeholder = vec.get(i); // Swap! vec.set(i, largestSoFar); vec.set(indexOfLSF, placeholder); } //find the smallest value in positions i to length-1 //swap it with the value at position i } }