// SortingDemoUnchecked.java
// Demonstrates selectionSort and isSorted methods,
// and facilitates testing of various things that
// can go wrong.
//
// In this version, the selectionSort method returns
// a boolean value indicating whether it was successful.
//
// See comments in main method for testing options.
/**
* 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 SortingDemoBoolean
{
/**
* 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 if possible.
// Otherwise, print an error message and quit.
if ( ! selectionSort(numbers, lengthFilled) )
{
System.out.println("Attempt to sort numbers failed.");
return;
} // if
// 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
// block containing a 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
* Must be between 0 and the length
* of <code>array</code>, inclusive.
*
* @return true if the array was successfully sorted,
* false otherwise (e.g. if the specified
* length was out of range).
*/
private static boolean selectionSort(short[] array,
int length)
{
if ( length < 0 || length > array.length )
return false; // to indicate error
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
// Was the sorting successful?
return isSorted(array, length);
} // 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.
*/
private static boolean isSorted(short[] array,
int length)
{
for ( int i = 1; i < length; i++ )
if ( array[i] < array[i-1] )
return false;
return true;
} // method isSorted
} // class SortingDemoBoolean