1 / 141

Heapsort

Heapsort. Section 8.8. Heapsort. Merge sort time is O( n log n ) but still requires, temporarily, n extra storage locations Heapsort does not require any additional storage As its name implies, heapsort uses a heap to store the array. First Version of a Heapsort Algorithm.

suki
Télécharger la présentation

Heapsort

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. Heapsort Section 8.8

  2. Heapsort • Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations • Heapsort does not require any additional storage • As its name implies, heapsort uses a heap to store the array

  3. First Version of a Heapsort Algorithm • When used as a priority queue, a heap maintains a smallest value at the top • The following algorithm • places an array's data into a heap, • then removes each heap item (O(n log n)) and moves it back into the array • This version of the algorithm requires n extra storage locations Heapsort Algorithm: First Version 1. Insert each value from the array to be sorted into a priority queue (heap). 2. Set i to 0 3. while the priority queue is not empty 4. Remove an item from the queue and insert it back into the array at position i 5. Increment i

  4. Revising the Heapsort Algorithm • In heaps we've used so far, each parent node value was less than the values of its children • We can build a heap so that each parent node value is larger than its children • Then, • move the top item to the bottom of the heap • reheap, ignoring the item moved to the bottom

  5. Trace of Heapsort 89 74 76 37 32 39 66 18 20 29 26 6 28 Go to End

  6. Trace of Heapsort (cont.) 89 74 76 37 32 39 66 18 20 29 26 6 28

  7. Trace of Heapsort (cont.) 89 74 76 37 32 39 66 18 20 29 26 6 28

  8. Trace of Heapsort (cont.) 6 74 76 37 32 39 66 18 20 29 26 89 28

  9. Trace of Heapsort (cont.) 76 74 6 37 32 39 66 18 20 29 26 89 28

  10. Trace of Heapsort (cont.) 76 74 37 6 32 39 66 18 20 29 26 89 28

  11. Trace of Heapsort (cont.) 76 74 37 26 32 39 66 18 20 29 6 89 28

  12. Trace of Heapsort (cont.) 76 74 37 26 32 39 66 18 20 29 6 89 28

  13. Trace of Heapsort (cont.) 76 74 37 26 32 39 66 18 20 29 6 89 28

  14. Trace of Heapsort (cont.) 76 74 37 26 32 39 66 18 20 29 6 89 28

  15. Trace of Heapsort (cont.) 29 74 37 26 32 39 66 18 20 76 6 89 28

  16. Trace of Heapsort (cont.) 74 29 37 26 32 39 66 18 20 76 6 89 28

  17. Trace of Heapsort (cont.) 74 66 37 26 32 39 29 18 20 76 6 89 28

  18. Trace of Heapsort (cont.) 74 66 37 26 32 39 29 18 20 76 6 89 28

  19. Trace of Heapsort (cont.) 74 66 37 26 32 39 29 18 20 76 6 89 28

  20. Trace of Heapsort (cont.) 74 66 37 26 32 39 29 18 20 76 6 89 28

  21. Trace of Heapsort (cont.) 28 66 37 26 32 39 29 18 20 76 6 89 74

  22. Trace of Heapsort (cont.) 66 28 37 26 32 39 29 18 20 76 6 89 74

  23. Trace of Heapsort (cont.) 66 39 37 26 32 28 29 18 20 76 6 89 74

  24. Trace of Heapsort (cont.) 66 39 37 26 32 28 29 18 20 76 6 89 74

  25. Trace of Heapsort (cont.) 66 39 37 26 32 28 29 18 20 76 6 89 74

  26. Trace of Heapsort (cont.) 66 39 37 26 32 28 29 18 20 76 6 89 74

  27. Trace of Heapsort (cont.) 18 39 37 26 32 28 29 66 20 76 6 89 74

  28. Trace of Heapsort (cont.) 39 18 37 26 32 28 29 66 20 76 6 89 74

  29. Trace of Heapsort (cont.) 39 29 37 26 32 28 18 66 20 76 6 89 74

  30. Trace of Heapsort (cont.) 39 29 37 26 32 28 18 66 20 76 6 89 74

  31. Trace of Heapsort (cont.) 39 29 37 26 32 28 18 66 20 76 6 89 74

  32. Trace of Heapsort (cont.) 39 29 37 26 32 28 18 66 20 76 6 89 74

  33. Trace of Heapsort (cont.) 6 29 37 26 32 28 18 66 20 76 39 89 74

  34. Trace of Heapsort (cont.) 37 29 6 26 32 28 18 66 20 76 39 89 74

  35. Trace of Heapsort (cont.) 37 29 32 26 6 28 18 66 20 76 39 89 74

  36. Trace of Heapsort (cont.) 37 29 32 26 6 28 18 66 20 76 39 89 74

  37. Trace of Heapsort (cont.) 37 29 32 26 6 28 18 66 20 76 39 89 74

  38. Trace of Heapsort (cont.) 37 29 32 26 6 28 18 66 20 76 39 89 74

  39. Trace of Heapsort (cont.) 20 29 32 26 6 28 18 66 37 76 39 89 74

  40. Trace of Heapsort (cont.) 32 29 20 26 6 28 18 66 37 76 39 89 74

  41. Trace of Heapsort (cont.) 32 29 26 20 6 28 18 66 37 76 39 89 74

  42. Trace of Heapsort (cont.) 32 29 26 20 6 28 18 66 37 76 39 89 74

  43. Trace of Heapsort (cont.) 32 29 26 20 6 28 18 66 37 76 39 89 74

  44. Trace of Heapsort (cont.) 32 29 26 20 6 28 18 66 37 76 39 89 74

  45. Trace of Heapsort (cont.) 18 29 26 20 6 28 32 66 37 76 39 89 74

  46. Trace of Heapsort (cont.) 29 18 26 20 6 28 32 66 37 76 39 89 74

  47. Trace of Heapsort (cont.) 29 28 26 20 6 18 32 66 37 76 39 89 74

  48. Trace of Heapsort (cont.) 29 28 26 20 6 18 32 66 37 76 39 89 74

  49. Trace of Heapsort (cont.) 29 28 26 20 6 18 32 66 37 76 39 89 74

  50. Trace of Heapsort (cont.) 29 28 26 20 6 18 32 66 37 76 39 89 74

More Related