01: public class QuickSorter
02: {
03:    public QuickSorter(int[] anArray)
04:    {
05:       a = anArray;
06:    }   
07: 
08:    /**
09:       Sorts the array managed by this merge sorter
10:    */
11:    public void sort()
12:    {  
13:       sort(0, a.length - 1);
14:    }
15: 
16:    public void sort(int from, int to)
17:    {
18:       if (from >= to) return;
19:       int p = partition(from, to);
20:       sort(from, p);
21:       sort(p + 1, to);
22:    }
23: 
24:    private int partition(int from, int to)
25:    {
26:       int pivot = a[from];
27:       int i = from - 1;
28:       int j = to + 1;
29:       while (i < j)
30:       {
31:          i++; while (a[i] < pivot) i++;
32:          j--; while (a[j] > pivot) j--;
33:          if (i < j) swap(i, j); 
34:       }
35:       return j;
36:    }
37: 
38:    /**
39:       Swaps two entries of the array.
40:       @param i the first position to swap
41:       @param j the second position to swap
42:    */
43:    private void swap(int i, int j)
44:    {
45:       int temp = a[i];
46:       a[i] = a[j];
47:       a[j] = temp;
48:    }
49: 
50:    private int[] a;
51: }