UNB/ CS/ David Bremner/ teaching/ java/ SortTime.java
import ccj.*;

public class SortTime
{  public static void main(String[] args)
   {  System.out.print("Enter array size: ");
      int n = Console.in.readInt();
      int[] v = new int[n];
      int i;
      for (i = 0; i < v.length; i++)
         v[i] = Numeric.randomInt(1, 100);
      Time before = new Time();
      selectionSort(v);
      Time after = new Time();
      System.out.println("Elapsed time = "
         + after.secondsFrom(before)
         + " seconds");
   }

   public static void swap(int[] a, int i, int j)
   {  int temp = a[i];
      a[i] = a[j];
      a[j] = temp;
   }

   public static void selectionSort(int[] a)
   {  int next;
         /* the next position to be set to the minimum */

      for (next = 0; next < a.length - 1; next++)
      {  /* find the position of the minimum */
         int minPos = next;
         int i;
         for (i = next + 1; i < a.length; i++)
            if (a[i] < a[minPos]) minPos = i;

         if (minPos != next)
            swap(a, minPos, next);
      }
   }
}