/**
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *   
 *
 * <p>
 * This class includes sorting routines
 * as described in Section 10.2.
 *
 * NOTE: This class is for illustration purpose only. Since
 *       the class does not have any instance variables to
 *       maintain, so it's more reasonable to implement this
 *       class an non-instantiable class. This class is
 *       implemented as an instantiable class, however, to
 *       make the method declarations match those given in
 *       the textboo.
 *
 * @author Dr. Caffeine
 */

class SortingRoutines
{
   

//----------------------------------
//    Constructors
//----------------------------------
                                
    /**
     * Default constructor
     */
    public SortingRoutines( )
    {
       
    }
   
//-------------------------------------------------
//      Public Methods:
// 
//          int   selectionSort (   int[]      )
//          int   bubbleSort    (   int[]      )
//
//------------------------------------------------
                                
    /**
     * Sorts the passed array using
     * the selection sort method.
     * 
     * @param number      an array of int to search
     *
     */
    public void selectionSort( int[] number )
    {
        int startIndex, minIndex, length, temp, i;
        length = number.length;
    
        for (startIndex = 0; startIndex <= length-2; startIndex++) {
            
             //each iteration of the for loop is one pass
    
            minIndex = startIndex;
    
            //find the smallest in this pass at 
            //position minIndex
            for (i = startIndex+1; i <= length-1; i++) {
                if (number[i] < number[minIndex]) minIndex = i;
            }
            
            //exchange number[startIndex] and number[minIndex]
            temp               = number[startIndex];
            number[startIndex] = number[minIndex];
            number[minIndex]   = temp;
        }
    }
    
    /**
     * Sorts the array using
     * the bubble sort method.
     * 
     * @param number      an array of int to sort
     *
     */
    public void bubbleSort( int[] number )
    {
        int     temp, bottom, i; 
        
        boolean exchanged = true;
    
        bottom = number.length - 2;
        
        while ( exchanged )  {        
    
            exchanged = false; 
    
            for (i = 0; i <= bottom; i++) {
                if (number[i] > number[i+1]) {
    
                    temp        = number[i];   //exchange
                    number[i]   = number[i+1];
                    number[i+1] = temp;
    
                    exchanged   = true; //exchange is made 
                }
            }
            
            bottom--;
        }
    }
   
}                   