// Routines to sort arrays of integers. // (c) 1997 duane a. bailey import structure.*; public class BubbleSort { //+bubbleSort public static void bubbleSort(int data[], int n) // pre: 0 <= n <= data.length // post: values in data[0..n-1] in ascending order { int numSorted = 0; // number of values in order int index; // general index while (numSorted < n) { // bubble a large element to higher array index for (index = 1; index < n-numSorted; index++) { if (data[index] < data[index-1]) swap(data,index,index-1); } // at least one more value in place numSorted++; } } //-bubbleSort //+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 { //+symmetry //+symmetryA //+symmetryB int temp; temp = data[i]; data[i] = data[j]; data[j] = temp; //-symmetry //-symmetryA //-symmetryB } //-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 }