/* Author : Michael Robinson Program : StringBubleSort.java CLASS NO MAIN METHOD. Purpose : This CLASS performs the bubble sort algorithm for String arrays. Note: There is no main in this CLASS. It can be accessed from any program. Updated : July 22, 2099 */ public class StringBubbleSort { public static void bubbleSort( String array[] ) { int maxElement; // Marks the last element to compare int index; // Index of an element to compare String temp; // Used to swap to elements // The outer loop positions maxElement at the last element // to compare during each pass through the array. Initially // maxElement is the index of the last element in the array. // During each iteration, it is decreased by one. System.out.print( "\n===============================================" ); for( maxElement = array.length - 1; maxElement >= 0; maxElement-- ) { // The inner loop steps through the array, comparing // each element with its neighbor. All of the elements // from index 0 through maxElement are involved in the // comparison. If two elements are out of order, they // are swapped. for( index = 0; index <= maxElement - 1; index++ ) { // Compare each element with its neighbor. if( array[index].compareTo(array[index + 1]) > 0 ) { // Swap the two elements. temp = array[index]; array[index] = array[index + 1]; array[index + 1] = temp; }//end if (array[index] > array[index + 1]) System.out.print( "\nmax,c=" + maxElement + "," + index + " " ); if( index < 10 ) { System.out.print( " " ); } showStep( array ); }//end inner loop: for( index =0; index <= maxElement- 1; //index++ ) }//end outer loop: for (maxElement = array.length - 1; //maxElement >= 0; maxElement--) }//end public static void bubbleSort(int array[]) public static void showStep( String array[] ) { System.out.print( " " ); for( String element : array ) //ENHANCED LOOP { System.out.print( element + " " ); } } }//end public class StringBubbleSort