next up previous contents
Next: Sortable Interface Up: MyUtil Package Previous: Parse Class

Sort Class

The Sort class will eventually contain a variety of sorting methods which are capable of sorting arrays of Sortable objects. It currently contains a heap sort.

package MyUtil;


public class Sort extends Object {

   static public void HeapSort(Sortable A[], int order) {
      int N = A.length;
      Sortable T;
      for(int k = N/2; k > 0; k--) {
         downheap(A, k, N, order); 
      }
      do {
         T = A[0];
         A[0] = A[N - 1];
         A[N - 1] = T;
         N = N - 1;
         downheap(A, 1, N, order);
      } while (N > 1);
   }

   static private void downheap(Sortable A[], int k, int N, int order) {
      Sortable T = A[k - 1];
      while (k <= N/2) {
         int j = k + k;
         if ((j < N) && A[j - 1].precedes(A[j],order))
            j++;
         if (!T.precedes(A[j-1],order))
            break;
         A[k - 1] = A[j - 1];
         k = j;
      }
      A[k - 1] = T;
   }

}



Kelly Waters
Mon Oct 27 18:18:15 EST 1997