1 / 31

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

titus
Télécharger la présentation

What else can we do with heaps?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What else can we do with heaps? Use the heap for sorting. Heapsort!

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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!

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

More Related