/* * File: Sort.java * Author: Java, Java, Java * Description: This class implements the Bubble Sort * algorithm. */ public class Sort { /** * bubbleSort() sorts the values in arr into ascending order * Pre: arr is not null. * Post: The values arr[0]...arr[arr.length-1] will be * arranged in ascending order. */ public void bubbleSort(int arr[]) { int temp; // Temporary variable for swap for (int pass = 1; pass < arr.length; pass++) // For each pass for (int pair = 1; pair < arr.length; pair++) // For each pair if (arr[pair-1] > arr[pair]) { // Compare temp = arr[pair-1]; // and swap arr[pair-1] = arr[pair]; arr[pair] = temp; } // if } // bubbleSort() /** * print() prints the values in an array * @param arr -- an array of integers */ public void print(int arr[]) { for (int k = 0; k < arr.length; k++) // For each integer System.out.print( arr[k] + " \t "); // Print it System.out.println(); } // print() /** * main() creates a Sort object and uses it to sort an array * of integers */ public static void main(String args[]) { int intArr[] = { 21, 20, 27, 24, 19 }; Sort sorter = new Sort(); sorter.print(intArr); sorter.bubbleSort(intArr); sorter.print(intArr); } // main() } //Sort