Presentation on theme: "What else can we do with heaps? Use the heap for sorting. Heapsort!"— Presentation transcript:
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