import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

/**
 * A program to demonstrate an obvious sorting method -- loading elements into a
 * heap and then pulling them out again in order from smallest to largest.
 * 
 * @author Mark Young (A00000000)
 */
public class AlmostHeapSort {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        List<Integer> numbers = 
                new ArrayList<>(Arrays.asList(12, 4, 5, 10, 88, 23, 42, 10));
        System.out.println("Before sorting: " + numbers);
        almostHeapSort(numbers);
        System.out.println("After sorting: " + numbers);
    }
    
    /**
     * Sort the elements of the given list by adding all its elements to a heap
     * then putting them back into the list in the order the heap removes them.
     * 
     * @param numbers the list to sort
     */
    private static void almostHeapSort(List<Integer> numbers) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        pq.addAll(numbers);
        numbers.clear();
        while (!pq.isEmpty()) {
            numbers.add(pq.remove());
        }
    }

}
