1 / 27

CS 261 – Data Structures

CS 261 – Data Structures. CS261 Data Structures . BuildHeap and Heapsort. Goals. Build a heap from an array of arbitrary values HeapSort algorithm Analysis of HeapSort. BuildHeap. How do we build a heap from an arbitrary array of values? ??. BuildHeap : Is this a proper heap?. 3 8.

macey-eaton
Télécharger la présentation

CS 261 – Data Structures

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. CS 261 – Data Structures CS261 Data Structures BuildHeap and Heapsort

  2. Goals • Build a heap from an array of arbitrary values • HeapSort algorithm • Analysis of HeapSort

  3. BuildHeap • How do we build a heap from an arbitrary array of values???

  4. BuildHeap: Is this a proper heap? 3 8 5 6 7 9 8 11 9 10 10 2 0 12 1 7 2 5 4 3 6 4 12 7 5 8 3 6 4 9 11 10 2 Are any of the subtreesguaranteed to be proper heaps?

  5. BuildHeap: Leaves are proper heaps 12 7 5 8 3 6 4 First non-leaf 9 11 10 2 3 8 5 6 7 9 8 11 9 10 10 2 0 12 1 7 2 5 4 3 6 4 11 Size = 11 Size/2 – 1 = 4

  6. BuildHeap • How can we use this information to build a heap from a random array? • _adjustHeap: takes a binary tree, rooted at a node, where all subtrees of that node are proper heaps and percolates down from that node to ensure that it is a proper heap • void _adjustHeap(structDynArr *heap,intmax, intpos) Adjust up to (not inclusive) Adjust from

  7. BuildHeap • Find the last non-leaf node,i, (going from left to right) • adjust heap from it to max • Decrement i and repeat until you process the root

  8. HeapSort • BuildHeap and _adjustHeap are the keys to an efficient , in-place, sorting algorithm • in-place means that we don’t require any extra storage for the algorithm • Any ideas???

  9. HeapSort • BuildHeap – turn arbitrary array into a heap • Swap first and last elements • Adjust Heap (from 0 to the last…not inclusive!) • Repeat 2-3 but decrement last each time through

  10. HeapSort Simulation: BuildHeap 3 4 5 7 0 9 1 3 2 8 4 5 9 Max = 6 i=Max/2 – 1 = 2 3 8 4 5 7 i=2 _adjustHeap(heap,6,2)

  11. HeapSort Simulation: BuildHeap 3 4 5 8 0 9 1 3 2 7 4 5 9 Max = 6 i=1 3 7 4 5 8 i=1 _adjustHeap(heap,6,1)

  12. HeapSort Simulation: BuildHeap 3 4 5 8 0 9 1 3 2 7 4 5 9 Max = 6 i= 0 3 7 4 5 8 i=0 _adjustHeap(heap,6,0)

  13. HeapSort Simulation: BuildHeap 3 9 5 8 0 3 1 4 2 7 4 5 3 4 7 9 5 8 i=-1 Done…with BuildHeap ….now let’s sort it!

  14. HeapSort • BuildHeap • Swap first and last • Adjust Heap (from 0 to the last) • Repeat 2-3 but decrement last

  15. HeapSort Simulation: Sort in Place 3 9 5 8 0 3 1 4 2 7 4 5 Iteration 1 3 4 7 9 5 8 i=5 Swap(v, 0, i) _adjustHeap(v, i, 0);

  16. HeapSort Simulation: Sort in Place Iteration 1 3 9 5 3 0 8 1 4 2 7 4 5 8 4 7 9 5 3 i=5 Swap(v, 0, i) _adjustHeap(v, i, 0);

  17. HeapSort Simulation: Sort in Place 3 9 5 3 0 4 1 5 2 7 4 8 Iteration 2 4 5 7 9 8 3 i=4 Swap(v, 0, i) _adjustHeap(v, i, 0);

  18. HeapSort Simulation: Sort in Place 3 9 5 3 0 8 1 5 2 7 4 4 Iteration 2 8 5 7 9 4 3 i=4 Swap(v, 0, i) _adjustHeap(v, i, 0);

  19. HeapSort Simulation: Sort in Place 3 9 5 3 0 5 1 8 2 7 4 4 Iteration 3 5 8 7 9 4 3 i=3 Swap(v, 0, i) _adjustHeap(v, i, 0);

  20. HeapSort Simulation: Sort in Place 3 5 5 3 0 9 1 8 2 7 4 4 Iteration 3 9 8 7 5 4 3 i=3 Swap(v, 0, i) _adjustHeap(v, i, 0);

  21. HeapSort Simulation: Sort in Place 3 5 5 3 0 7 1 8 2 9 4 4 Iteration 4 7 8 9 5 4 3 i=2 Swap(v, 0, i) _adjustHeap(v, i, 0);

  22. HeapSort Simulation: Sort in Place 3 5 5 3 0 9 1 8 2 7 4 4 Iteration 4 9 8 7 5 4 3 i=2 Swap(v, 0, i) _adjustHeap(v, i, 0);

  23. HeapSort Simulation: Sort in Place 3 5 5 3 0 8 1 9 2 7 4 4 Iteration 5 8 9 7 5 4 3 i=1 Swap(v, 0, i) _adjustHeap(v, i, 0);

  24. HeapSort Simulation: Sort in Place 3 5 5 3 0 9 1 8 2 7 4 4 Iteration 5 9 8 7 5 4 3 i=1 Swap(v, 0, i) _adjustHeap(v, i, 0);

  25. HeapSort Simulation: Sort in Place 3 5 5 3 0 9 1 8 2 7 4 4 9 8 7 5 4 3 i=0 DONE

  26. HeapSort Performance • Build Heap: n/2 calls to _adjustHeap = O(n log n) • HeapSort: n calls to swap and adjust = O(n log n) • Total: O(n log n)

  27. Your Turn • Worksheet 34 – BuildHeap and Heapsort

More Related