public class SelectionSort { /** Source: Savitch, Display 6.11 Precondition: numberUsed <= a.length; The first numberUsed indexed variables have values. Action: Sorts a so that a[0] <= a[1] <= a[2] ... <= a[numberUsed-1]; */ public static void sort(double[] a, int numberUsed) { int index, indexOfNextSmallest; for (index=0; index < numberUsed - 1; index++) {//Place the correct value in a[index]; indexOfNextSmallest = indexOfSmallest(index,a,numberUsed); interchange(index,indexOfNextSmallest, a); // a[0] <= a[1] ,,, < a[index] and these are the smallest // of the original array elements. The remaining positions // contain the rest of the original array elements. } } /** Returns the index of the smallest value among a[startIndex] ... a[numberUsed - 1] */ private static int indexOfSmallest(int startIndex, double[] a, int numberUsed) { double min = a[startIndex]; int indexOfMin = startIndex; int index; for (index = startIndex + 1; index < numberUsed; index++) if (a[index] < min) { min = a[index]; indexOfMin = index; // min is smallest of a[startIndex] through a[index] } return indexOfMin; } /** Precondition: i and j are legal indices for the array a. Postcondition: Values of a[i] and a[j] have been interchanged */ private static void interchange(int i, int j, double [] a) { double temp = a[i]; a[i] = a[j]; a[j] = temp; // original value of a[i] } }