what else can we do with heaps?
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/1.jpg)
What else can we do with heaps?
Use the heap for sorting.
Heapsort!
![Page 2: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/2.jpg)
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
![Page 3: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/3.jpg)
HeapSortafter BuildHeap
1
5 2
5 6 3 9
11 19 7 12 23 22 68 14
87 43 27 35 99 42 77 71 33 67 89
size = 26
![Page 4: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/4.jpg)
HeapSort
1
5 2
5 6 3 9
11 19 7 12 23 22 68 14
87 43 27 35 99 42 77 71 33 67 89
size = 26removeMin
![Page 5: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/5.jpg)
HeapSort
2
5 3
5 6 22 9
11 19 7 12 23 89 68 14
87 43 27 35 99 42 77 71 33 67
size = 25removeMin = 1
Not in heap now
![Page 6: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/6.jpg)
HeapSort
2
5 3
5 6 22 9
11 19 7 12 23 89 68 14
87 43 27 35 99 42 77 71 33 67 1
size = 25
heap is 1 element smaller, smallest element is at end of array
![Page 7: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/7.jpg)
HeapSort
2
5 3
5 6 22 9
11 19 7 12 23 89 68 14
87 43 27 35 99 42 77 71 33 67 1
size = 25
Now do it again!
![Page 8: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/8.jpg)
HeapSort
3
5 9
5 6 22 14
11 19 7 12 23 89 68 67
87 43 27 35 99 42 77 71 33 2 1
size = 24
![Page 9: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/9.jpg)
HeapSort
5
5 9
11 6 22 14
33 19 7 12 23 89 68 67
87 43 27 35 99 42 77 71 3 2 1
size = 23
![Page 10: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/10.jpg)
HeapSort
5
6 9
11 7 22 14
33 19 42 12 23 89 68 67
87 43 27 35 99 71 77 5 3 2 1
size = 22
![Page 11: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/11.jpg)
HeapSort
6
7 9
11 7 22 14
33 19 42 12 23 89 68 67
87 43 27 35 99 71 5 5 3 2 1
size = 21
![Page 12: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/12.jpg)
HeapSort
7
7 9
11 12 22 14
33 19 42 71 23 89 68 67
87 43 27 35 99 6 5 5 3 2 1
size = 20
![Page 13: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/13.jpg)
HeapSort
7
11 9
19 12 22 14
33 27 42 71 23 89 68 67
87 43 99 35 7 6 5 5 3 2 1
size = 19
![Page 14: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/14.jpg)
HeapSort
9
11 14
19 12 22 35
33 27 42 71 23 89 68 67
87 43 99 7 7 6 5 5 3 2 1
size = 18
![Page 15: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/15.jpg)
HeapSort
11
12 14
19 42 22 35
33 27 99 71 23 89 68 67
87 43 9 7 7 6 5 5 3 2 1
size = 17
![Page 16: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/16.jpg)
HeapSort
12
19 14
27 42 22 35
33 43 99 71 23 89 68 67
87 11 9 7 7 6 5 5 3 2 1
size = 16
![Page 17: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/17.jpg)
HeapSort
14
19 22
27 42 23 35
33 43 99 71 87 89 68 67
12 11 9 7 7 6 5 5 3 2 1
size = 15
![Page 18: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/18.jpg)
HeapSort
19
27 22
33 42 23 35
67 43 99 71 87 89 68 14
12 11 9 7 7 6 5 5 3 2 1
size = 14
![Page 19: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/19.jpg)
HeapSort
22
27 23
33 42 68 35
67 43 99 71 87 89 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 13
![Page 20: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/20.jpg)
HeapSort
23
27 35
33 42 68 89
67 43 99 71 87 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 12
![Page 21: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/21.jpg)
HeapSort
27
33 35
43 42 68 89
67 87 99 71 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 11
![Page 22: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/22.jpg)
HeapSort
33
42 35
43 71 68 89
67 87 99 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 10
![Page 23: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/23.jpg)
HeapSort
35
42 68
43 71 99 89
67 87 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 9
![Page 24: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/24.jpg)
HeapSort
42
43 68
67 71 99 89
87 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 8
![Page 25: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/25.jpg)
HeapSort
43
67 68
87 71 99 89
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 7
![Page 26: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/26.jpg)
HeapSort
67
71 68
87 89 99 43
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 6
![Page 27: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/27.jpg)
HeapSort
68
71 99
87 89 67 43
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 5
![Page 28: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/28.jpg)
HeapSort
71
87 99
89 68 67 43
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 4
![Page 29: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/29.jpg)
HeapSort
87
89 99
71 68 67 43
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 3
![Page 30: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/30.jpg)
HeapSort
89
99 87
71 68 67 43
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 2
![Page 31: What else can we do with heaps?](https://reader035.vdocument.in/reader035/viewer/2022081519/56814359550346895dafd78c/html5/thumbnails/31.jpg)
HeapSort
99
89 87
71 68 67 43
42 35 33 27 23 22 19 14
12 11 9 7 7 6 5 5 3 2 1
size = 1