Source of 27.20.java


  1: /** @author Frank M. Carrano, Timothy M. Henry
  2:     @version 5.0 */
  3: public static <T extends Comparable<? super T>>
  4:        void heapSort(T[] array, int n)
  5: {
  6:    // Create first heap
  7:    for (int rootIndex = n / 2 - 1; rootIndex >= 0; rootIndex--)
  8:       reheap(array, rootIndex, n - 1);

 10:    swap(array, 0, n - 1);

 12:    for (int lastIndex = n - 2; lastIndex > 0; lastIndex--)
 13:    {
 14:       reheap(array, 0, lastIndex);
 15:       swap(array, 0, lastIndex);
 16:    } // end for
 17: } // end heapSort