01: /**
02:    This class sorts an array, using the insertion sort 
03:    algorithm
04: */
05: public class InsertionSorter
06: {
07:    /**
08:       Constructs an insertion sorter.
09:       @param anArray the array to sort
10:    */   
11:    public InsertionSorter(int[] anArray)
12:    {
13:       a = anArray;
14:    }
15: 
16:    /**
17:       Sorts the array managed by this insertion sorter
18:    */      
19:    public void sort()
20:    {
21:       for (int i = 1; i < a.length; i++)
22:       {
23:          int next = a[i];
24:          // Move all larger elements up
25:          int j = i;
26:          while (j > 0 && a[j - 1] > next)
27:          {
28:             a[j] = a[j - 1];
29:             j--;
30:          }
31:          // Insert the element
32:          a[j] = next;
33:       }
34:    }
35:    
36:    private int[] a;
37: }