What else can we do with heaps?
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
What else can we do with heaps?
E N D
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
HeapSortafter BuildHeap size = 26 1 2 5 5 6 3 9 11 19 7 12 23 22 68 14 89 27 35 99 42 77 71 87 43 33 67
HeapSort size = 26 1 removeMin 2 5 5 6 3 9 11 19 7 12 23 22 68 14 89 27 35 99 42 77 71 87 43 33 67
HeapSort size = 25 2 removeMin = 1 3 5 5 6 22 9 11 19 7 12 23 89 68 14 Not in heap now 27 35 99 42 77 71 87 43 33 67
HeapSort size = 25 2 3 5 5 6 22 9 11 19 7 12 23 89 68 14 1 27 35 99 42 77 71 87 43 33 67 heap is 1 element smaller, smallest element is at end of array
HeapSort size = 25 2 3 5 5 6 22 9 11 19 7 12 23 89 68 14 1 27 35 99 42 77 71 87 43 33 67 Now do it again!
HeapSort size = 24 3 9 5 5 6 22 14 11 19 7 12 23 89 68 67 1 27 35 99 42 77 71 87 43 33 2
HeapSort size = 23 5 9 5 11 6 22 14 33 19 7 12 23 89 68 67 1 27 35 99 42 77 71 87 43 3 2
HeapSort size = 22 5 9 6 11 7 22 14 33 19 42 12 23 89 68 67 1 27 35 99 71 77 5 87 43 3 2
HeapSort size = 21 6 9 7 11 7 22 14 33 19 42 12 23 89 68 67 1 27 35 99 71 5 5 87 43 3 2
HeapSort size = 20 7 9 7 11 12 22 14 33 19 42 71 23 89 68 67 1 27 35 99 6 5 5 87 43 3 2
HeapSort size = 19 7 9 11 19 12 22 14 33 27 42 71 23 89 68 67 1 99 35 7 6 5 5 87 43 3 2
HeapSort size = 18 9 14 11 19 12 22 35 33 27 42 71 23 89 68 67 1 99 7 7 6 5 5 87 43 3 2
HeapSort size = 17 11 14 12 19 42 22 35 33 27 99 71 23 89 68 67 1 9 7 7 6 5 5 87 43 3 2
HeapSort size = 16 12 14 19 27 42 22 35 33 43 99 71 23 89 68 67 1 9 7 7 6 5 5 87 11 3 2
HeapSort size = 15 14 22 19 27 42 23 35 33 43 99 71 87 89 68 67 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 14 19 22 27 33 42 23 35 67 43 99 71 87 89 68 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 13 22 23 27 33 42 68 35 67 43 99 71 87 89 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 12 23 35 27 33 42 68 89 67 43 99 71 87 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 11 27 35 33 43 42 68 89 67 87 99 71 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 10 33 35 42 43 71 68 89 67 87 99 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 9 35 68 42 43 71 99 89 67 87 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 8 42 68 43 67 71 99 89 87 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 7 43 68 67 87 71 99 89 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 6 67 68 71 87 89 99 43 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 5 68 99 71 87 89 67 43 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 4 71 99 87 89 68 67 43 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 3 87 99 89 71 68 67 43 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 2 89 87 99 71 68 67 43 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2
HeapSort size = 1 99 87 89 71 68 67 43 42 35 33 27 23 22 19 14 1 9 7 7 6 5 5 12 11 3 2