Source of SelectionSort.java



  3: import java.util.Scanner;

  5: /**
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class SelectionSort {

 11:     public static final Scanner KBD = Common.KBD;
 12:     private static final int HOW_MANY = 10;
 13:     private static final int MAX = 1000;
 14:     private static int traceLevel = 0;

 16:     public static void main(String[] args) {
 17:         System.out.println("\n\n"
 18:                 + "Selection Sort\n"
 19:                 + "==============\n");

 21:         setTraceLevel();

 23:         // create an array of random integers
 24:         int[] numbers = Common.randomNumbers(HOW_MANY, MAX / 10, MAX);
 25:         Common.printArray(numbers);
 26:         Common.pause();

 28:         // sort it
 29:         selectionSort(numbers);

 31:         // show it sorted
 32:         System.out.println("Array now sorted");
 33:         Common.printArray(numbers);
 34:         Common.pause();
 35:     }

 37:     /**
 38:      * Prompt for and read a level of tracing to do.
 39:      */
 40:     public static void setTraceLevel() {
 41:         String traceMenu = "Enter a trace level: \n"
 42:                 + "  0 - no tracing\n"
 43:                 + "  1 - outer loop only\n"
 44:                 + "  2 - inner loop as well\n\n"
 45:                 + "Trace level: ";

 47:         System.out.print(traceMenu);
 48:         traceLevel = KBD.nextInt();
 49:         KBD.nextLine();
 50:         while (traceLevel < 0 || 2 < traceLevel) {
 51:             System.out.print(traceMenu);
 52:             traceLevel = KBD.nextInt();
 53:             KBD.nextLine();
 54:         }
 55:     }

 57:     /**
 58:      * Perform insertion sort on the given array.
 59:      *
 60:      * @param arr the array to sort
 61:      */
 62:     public static void selectionSort(int[] arr) {
 63:         for (int i = 0; i < arr.length - 1; ++i) {
 64:             int p = i;
 65:             for (int j = i + 1; j < arr.length; ++j) {
 66:                 if (traceLevel > 1) {
 67:                     System.out.println("...looking for smaller than " + arr[p]
 68:                             + "...");
 69:                     Common.printTwoOf(arr, j, p);
 70:                     Common.pause();
 71:                 }
 72:                 if (arr[j] < arr[p]) {
 73:                     p = j;
 74: //                    if (traceLevel > 1) {
 75: //                        System.out.println("...found new smallest: " + arr[p] 
 76: //                                + "...");
 77: //                        Common.pause();
 78: //                    }
 79:                 }
 80:             }
 81:             Common.swap(arr, i, p);
 82:             if (traceLevel > 1) {
 83:                 System.out.println("...swapped smallest into position...");
 84:                 Common.printTwoOf(arr, i, p);
 85:                 Common.pause();
 86:             }
 87:             if (traceLevel > 0) {
 88:                 System.out.println("one more selected: ");
 89:                 Common.printArray(arr, 0, i + 1);
 90:                 Common.pause();
 91:             }
 92:         }
 93:     }

 95: }