// Routines to sort arrays of integers. // (c) 1997 duane a. bailey import structure.*; public class SelectionSort { //+selectionSort public static void selectionSort(int data[], int n) // pre: 0 <= n <= data.length // post: values in data[0..n-1] are in ascending order { int numUnsorted = n; //+selectMaximum int index; // general index int max; // index of largest value //-selectMaximum while (numUnsorted > 0) { //+selectMaximum // determine maximum value in array max = 0; for (index = 1; index < numUnsorted; index++) { if (data[max] < data[index]) max = index; } //-selectMaximum swap(data,max,numUnsorted-1); numUnsorted--; } } //-selectionSort //+swap public static void swap(int data[], int i, int j) // pre: 0 <= i,j < data.length // post: data[i] and data[j] are exchanged { int temp; temp = data[i]; data[i] = data[j]; data[j] = temp; } //-swap //+vectorSort public static void swap(Vector data, int i, int j) // pre: 0 <= i,j < data.size() // post: i-th j-th elements of data are exchanged { Object temp; temp = data.elementAt(i); data.setElementAt(data.elementAt(j),i); data.setElementAt(temp,j); } //-vectorSort }