[p.1310-ed.3] Exercise 25.5 [modified by L. Lilien] (Bubble Sort a.k.a. Sinking Sort) Implement the bubble sort — another simple, yet inefficient, sorting technique. It is called bubble sort or sinking sort because smaller values gradually “bubble” their way to the top of the array (i.e., toward the first element) like air bubbles rising in water, while the larger values sink to the bottom (end) of the array. The technique uses nested loops to make several passes through the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the bubble sort leaves the values as they are. If a pair is in decreasing order, the bubble sort swaps their values in the array. The first pass compares the first two elements of the array and swaps them if necessary. It then compares the second and third elements. The end of this pass compares the last two elements in the array and swaps them if necessary. After one pass, the largest element will be in the last position. After two passes, the largest two elements will be in the last two positions, etc. in heneral, after i passes, the largest (the "heaviest") i elements will be in the last i positions ("at the i bottom positions "). A note on the algorithn time complexity: Bubble sort is an O(n**2) algorithm. It contains two nested for loops. The outer for loop iterates over data. Length - 1 passes. The inner for loop (lines 27–32) iterates over data. Length - 1 elements in the array. When loops are nested, the respective orders are multiplied. This is because for each of O(n) iterations of the outside loop, there are O(n) iterations of the inner loop. This results in a total Big O of O(n**2). ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // MODIFIED BY L. LILIEN for CS 1120, WMU (v.2010.04.20-1) // Exercise 25.5 Solution: BubbleSort.cs // Sort an array's values into ascending order. using System; public class BubbleSort { private int[] data; // array of values private static Random generator = new Random(); // create array of given size and fill with random integers public BubbleSort( int size ) { data = new int[ size ]; // create space for array // fill array with random ints in range 10-99 for ( int i = 0; i < size; i++ ) data[ i ] = generator.Next( 10, 100 ); } // end BubbleSort constructor // sort elements of array with bubble sort public void Sort() { // loop for data.Length - 1 passes for ( int pass = 1; pass < data.Length; pass++ ) { // loop over elements in array for ( int index = 0; index < data.Length - 1; index++ ) { // swap adjacent elements if first is greater than second if ( data[ index ] > data[ index + 1 ] ) Swap( index, index + 1 ); // swap adjacent elements } // end inner for } // end outer for } // end method Sort // helper method to swap values in two elements public void Swap( int first, int second ) { int temporary = data[ first ]; // store first in temporary data[ first ] = data[ second ]; // replace first with second data[ second ] = temporary; // put temporary in second // print intermediate array Console.WriteLine("Swapped: " + data[second] + " and " + data[first]); foreach (var element in data) { Console.Write(element + " "); } Console.WriteLine(""); } // end method Swap // method to output values in array public override string ToString() { string temporary = string.Empty; // iterate through array foreach ( var element in data ) temporary += element + " "; temporary += "\n"; // add newline character return temporary; } // end method ToString } // end class BubbleSort /************************************************************************** * (C) Copyright 1992-2009 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // MODIFIED by L. Lilien for CS 1120, Western Michigan University (v.2010.04.20-1) // Exercise 25.5 Solution: BubbleSortTest.cs // Test the bubble sort class. using System; public class BubbleSortTest { public static void Main( string[] args ) { // create object to perform bubble sort BubbleSort sortArray = new BubbleSort( 10 ); Console.WriteLine( "Before:" ); Console.WriteLine( sortArray ); // display unsorted array sortArray.Sort(); // sort array Console.WriteLine( "\nAfter:" ); Console.WriteLine( sortArray ); // display sorted array } // end Main } // end class BubbleSortTest /************************************************************************** * (C) Copyright 1992-2009 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Sample output: ============== Before: 16 67 49 98 89 87 52 92 82 50 Swapped: 67 and 49 16 49 67 98 89 87 52 92 82 50 Swapped: 98 and 89 16 49 67 89 98 87 52 92 82 50 Swapped: 98 and 87 16 49 67 89 87 98 52 92 82 50 Swapped: 98 and 52 16 49 67 89 87 52 98 92 82 50 Swapped: 98 and 92 16 49 67 89 87 52 92 98 82 50 Swapped: 98 and 82 16 49 67 89 87 52 92 82 98 50 Swapped: 98 and 50 16 49 67 89 87 52 92 82 50 98 Swapped: 89 and 87 16 49 67 87 89 52 92 82 50 98 Swapped: 89 and 52 16 49 67 87 52 89 92 82 50 98 Swapped: 92 and 82 16 49 67 87 52 89 82 92 50 98 Swapped: 92 and 50 16 49 67 87 52 89 82 50 92 98 Swapped: 87 and 52 16 49 67 52 87 89 82 50 92 98 Swapped: 89 and 82 16 49 67 52 87 82 89 50 92 98 Swapped: 89 and 50 16 49 67 52 87 82 50 89 92 98 Swapped: 67 and 52 16 49 52 67 87 82 50 89 92 98 Swapped: 87 and 82 16 49 52 67 82 87 50 89 92 98 Swapped: 87 and 50 16 49 52 67 82 50 87 89 92 98 Swapped: 82 and 50 16 49 52 67 50 82 87 89 92 98 Swapped: 67 and 50 16 49 52 50 67 82 87 89 92 98 Swapped: 52 and 50 16 49 50 52 67 82 87 89 92 98 After: 16 49 50 52 67 82 87 89 92 98 Press any key to continue . . .