01: /**
02:    This class sorts an array, using the selection sort 
03:    algorithm
04: */
05: public class SelectionSorter
06: {
07:    /**
08:       Constructs a selection sorter.
09:       @param anArray the array to sort
10:    */
11:    public SelectionSorter(int[] anArray)
12:    {
13:       a = anArray;
14:    }
15: 
16:    /**
17:       Sorts the array managed by this selection sorter.
18:    */
19:    public void sort()
20:    {  
21:       for (int i = 0; i < a.length - 1; i++)
22:       {  
23:          int minPos = minimumPosition(i);
24:          swap(minPos, i);
25:       }
26:    }
27: 
28:    /**
29:       Finds the smallest element in a tail range of the array.
30:       @param from the first position in a to compare
31:       @return the position of the smallest element in the
32:       range a[from] . . . a[a.length - 1]
33:    */
34:    private int minimumPosition(int from)
35:    {  
36:       int minPos = from;
37:       for (int i = from + 1; i < a.length; i++)
38:          if (a[i] < a[minPos]) minPos = i;
39:       return minPos;
40:    }
41: 
42:    /**
43:       Swaps two entries of the array.
44:       @param i the first position to swap
45:       @param j the second position to swap
46:    */
47:    private void swap(int i, int j)
48:    {
49:       int temp = a[i];
50:       a[i] = a[j];
51:       a[j] = temp;
52:    }
53: 
54:    private int[] a;
55: }