// SortingDemoAssertions1.java
// Demonstrates selectionSort and isSorted methods.
// Uses assertions to detect various programmer errors.
// See comments in main method for testing options.
//
// To compile this program in Java 1.4, type:
//    javac -source 1.4 SortingDemoAssertions1.java
// (Compile it normally in Java 1.5 or later.)
//
// To run this program with assertions enabled, type:
//    java -ea SortingDemoAssertions1
// (Otherwise, assertions are disabled.)


/**
 * Test program for selectionSort and isSorted
 * methods defined within this class.  Main
 * method contains commented-out options to
 * facilitate testing of parameter values
 * including inappropriate and boundary cases.
 *
 * @author Dorothy L. Nixon
 */
public class SortingDemoAssertions1
{
   /**
    * Main method.
    *
    * @param args command-line arguments.  None needed;
    */
   public static void main(String[] args)
   {
      short[] numbers = new short[] {5, -1, 9, 4, 8, 2, -3, 10, 3};
      int lengthFilled = 6;

      // To test various alternative values of numbers,
      // both appropriate and inappropriate, uncomment one
      // of the following lines at a time:
      //
      // numbers = new short[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
      // numbers = new short[] {9, 8, 7, 6, 5, 4, 3, 2, 1};
      // numbers = new short[] {2, 1};
      // numbers = new short[] {1};         // trivial array (okay)
      // numbers = new short[] {};          // empty array (okay)
      // numbers = null;                    // no array (not okay)

      // To test various alternative values of lengthFilled,
      // both appropriate and inappropriate, uncomment one
      // of the following lines at a time:
      //
      // lengthFilled = 0;                    // appropriate
      // lengthFilled = numbers.length;       // appropriate
      // lengthFilled = -1;                   // inappropriate
      // lengthFilled = numbers.length + 1;   // inappropriate

      // Sort numbers in ascending order.  Test selectionSort:
      selectionSort(numbers, lengthFilled);

      // Below is a test of the isSorted method, which
      // determines whether a subarray is sorted.  To test
      // isSorted for unsorted arrays, comment out the above
      // call to selectionSort.
      System.out.println(
                  "According to the isSorted method, the array "
                  + ((isSorted(numbers, lengthFilled))?"IS":"is NOT")
                  + " sorted.");

      // Display the numbers:
      System.out.print("End test.  Array contains: ");
      for ( int i = 0; i < lengthFilled; i++ )
         System.out.print(" " + numbers[i]);
      System.out.println();
   }  // method main

   /**
    * 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
    */
   private static void selectionSort(short[] array,
                                     int length)
   {
      // Does array exist?
      assert ( array != null ) : "parameter array is null";

      // Is length within range?
      assert ( length >= 0 && length <= array.length ) :
             "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.
    */
   private static boolean isSorted(short[] array,
                                   int length)
   {
      // Does array exist?
      assert ( array != null ) : "parameter array is null";

      // Is length within range?
      assert ( length >= 0 && length <= array.length ) :
             "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 SortingDemoAssertions1