What else can we do with heaps? Use the heap for sorting. Heapsort!
Basic Idea Given an unsorted array we use BuildHeap to convert it into a heap While(heap is not empty) –removeMin –The heap is one smaller but the array hasn't changed –Put the item just removed in the element just after the end of the heap At conclusion the array is sorted
HeapSort after BuildHeap size = 26
HeapSort size = 26 removeMin
HeapSort size = 25 removeMin = 1 Not in heap now
HeapSort size = 25 heap is 1 element smaller, smallest element is at end of array
HeapSort size = 25 Now do it again!
HeapSort size = 24
HeapSort size = 23
HeapSort size = 22
HeapSort size = 21
HeapSort size = 20
HeapSort size = 19
HeapSort size = 18
HeapSort size = 17
HeapSort size = 16
HeapSort size = 15
HeapSort size = 14
HeapSort size = 13
HeapSort size = 12
HeapSort size = 11
HeapSort size = 10
HeapSort size = 9
HeapSort size = 8
HeapSort size = 7
HeapSort size = 6
HeapSort size = 5
HeapSort size = 4
HeapSort size = 3
HeapSort size = 2
HeapSort size = 1