@ Override public < E > List < E > sort ( final List < E > elements ) { int size = elements . size ( ) ; // create the heap for ( int parentIndex = ( ( size - 2 ) / 2 ) ; parentIndex >= 0 ; parentIndex -- ) { siftDown ( elements , parentIndex , size - 1 ) ; } // swap the first and last elements in the heap of array since the first is the largest value in the heap, // and then heapify the heap again for ( int count = ( size - 1 ) ; count > 0 ; count -- ) { swap ( elements , 0 , count ) ; siftDown ( elements , 0 , count - 1 ) ; } return elements ; }
Uses the Heap Sort algorithm to sort a List of elements as defined by the Comparator or as determined by the elements in the collection if the elements are Comparable .