// SortingDemoExceptions.java
// Contains public static selectionSort and isSorted
// methods.  Uses exceptions for inappropriate
// parameter values, and uses an assertion to check
// postcondition in selectionSort.
//
// To compile this class in Java 1.4, type:
//    javac -source 1.4 SortingDemoExceptions.java
// (Compile it normally in Java 1.5 or later.)


/**
 * Contains static methods for sorting an array
 * and determining whether an array is sorted.
 *
 * @author Dorothy L. Nixon
 */
public class SortingDemoExceptions
{
   /**
    * Sorts a partially-filled array of short
    * integers using selection sort.
    *
    * @param array the array to be sorted.
    * @param length length of filled portion of array.
    *               Must be between 0 and the length
    *               of <code>array</code>, inclusive.
    *
    * @exception NullPointerException if <code>array</code>
    *                 is null
    * @exception IndexOutOfBoundsException if
    *                 <code>length</code> is outside the
    *                 range of possible subarray lengths
    *                 of <code>array</code>
    */
   public static void selectionSort(short[] array, int length)
   {
      // Does array exist?
      if ( array == null )
         throw new NullPointerException("parameter array is null");

      // Is length within range?
      if ( length < 0 || length > array.length )
         throw new IndexOutOfBoundsException("length " + length
                                             + " not in range [0, "
                                             + array.length + "]");

      // Sort the array:
      for ( int i = 0; i < length - 1; i++ )
      {
         // Find the lowest-valued element in
         // the subarray from index i up to
         // index length - 1
         int indexLowest = i;
         for ( int j = i + 1; j < length; j++ )
            if ( array[j] < array[indexLowest] )
               indexLowest = j;

         // Put the lowest-valued element at
         // index i, swapping if necessary:
         if ( array[indexLowest] < array[i] )
         {
            short temp = array[indexLowest];
            array[indexLowest] = array[i];
            array[i] = temp;
         }
      }  // for i

      // Has the array been sorted successfully?
      assert ( isSorted(array, length) ): "Array not sorted.";
   }  // method selectionSort

   /**
    * Detects whether the specified subarray is sorted
    * in ascending order.
    *
    * @param array an array containing the subarray to be checked.
    *            Assume that the subarray starts at index 0.
    * @param length length of the subarray.
    *               Must be between 0 and the length
    *               of <code>array</code>, inclusive.
    *
    * @exception NullPointerException if <code>array</code>
    *                 is null
    * @exception IndexOutOfBoundsException if
    *                 <code>length</code> is outside the
    *                 range of possible subarray lengths
    *                 of <code>array</code>
    */
   public static boolean isSorted(short[] array, int length)
   {
      // Does array exist?
      if ( array == null )
         throw new NullPointerException("parameter array is null");

      // Is length within range?
      if ( length < 0 || length > array.length )
         throw new IndexOutOfBoundsException("length " + length
                                             + " not in range [0, "
                                             + array.length + "]");

      // If any element in the array is less than
      // the preceding element, then it is not sorted.
      // Otherwise, it is sorted.
      for ( int i = 1; i < length; i++ )
         if ( array[i] < array[i-1] )
            return false;
      return true;
   }  // method isSorted
}  // class SortingDemoExceptions