1 / 43

Heap Sort

Heap Sort. CS 105. Using a heap to sort. Using a heap-based priority queue, sorting can be done in O( n log n ) time Insert all n elements in the priority queue Repeatedly remove the elements from the priority queue (they will come out sorted)

habib
Download Presentation

Heap Sort

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. Heap Sort CS 105

  2. Using a heap to sort • Using a heap-based priority queue, sorting can be done in O( n log n ) time • Insert all n elements in the priority queue • Repeatedly remove the elements from the priority queue (they will come out sorted) • 2n operations on the priority queue, each taking O( log n ) time => O( n log n ) • Sorting can be done within the array by treating the array as a heap

  3. Heap sort • Reverse the heap property: child nodes should always be less than the parent nodes • Phase 1: convert the array into an n-element heap • Phase 2: repeatedly remove maximum element from the heap, and place that element in its proper position in the array • swap element at 0th position with element at (n-1)th position and then “reheapify” considering only the first n-1 elements • repeat this process until heap size is reduced to 1 (minimum element remains, at 0th position)

  4. Heap sort example 6 10 5 9 20 12 3 2 15 8 18

  5. Phase 1: build the heap for i  1 to n-1 do insert element s[i] into the heap consisting of the elements s[0]…s[i-1] - Once the heap is built, s[0] will contain the maximum element

  6. Phase 1: build heap 6 10 5 9 20 12 3 2 15 8 18

  7. Phase 1: build heap 10 6 5 9 20 12 3 2 15 8 18

  8. Phase 1: build heap 10 6 5 9 20 12 3 2 15 8 18

  9. Phase 1: build heap 12 10 5 9 20 6 3 2 15 8 18

  10. Phase 1: build heap 12 10 5 9 20 6 3 2 15 8 18

  11. Phase 1: build heap 12 10 9 5 20 6 3 2 15 8 18

  12. Phase 1: build heap 20 10 12 5 9 6 3 2 15 8 18

  13. Phase 1: build heap 20 10 12 5 9 6 3 2 15 8 18

  14. Phase 1: build heap 20 15 12 5 9 10 3 2 6 8 18

  15. Phase 1: build heap 20 15 12 5 9 10 8 2 6 3 18

  16. Phase 1: build heap 20 Maximum element 18 12 5 9 10 15 2 6 3 8

  17. Phase 2: repeatedly select max for i  n-1 down to 1 do swap s[0] and s[i]“demote” s[0] to its proper place in the heap consisting of the elements s[0]...s[i-1]

  18. Phase 2: select max 20 18 12 5 9 10 15 2 6 3 8

  19. Phase 2: select max 8 18 12 5 9 10 15 2 6 3 20

  20. Phase 2: select max 18 15 12 5 9 10 8 2 6 3 20

  21. Phase 2: select max 18 15 12 5 9 10 8 2 6 3 20

  22. Phase 2: select max 3 15 12 5 9 10 8 2 6 18 20

  23. Phase 2: select max 15 10 12 5 9 6 8 2 3 18 20

  24. Phase 2: select max 15 10 12 5 9 6 8 2 3 18 20

  25. Phase 2: select max 3 10 12 5 9 6 8 2 15 18 20

  26. Phase 2: select max 12 10 9 5 3 6 8 2 15 18 20

  27. Phase 2: select max 12 10 9 5 3 6 8 2 15 18 20

  28. Phase 2: select max 2 10 9 5 3 6 8 12 15 18 20

  29. Phase 2: select max 10 8 9 5 3 6 2 12 15 18 20

  30. Phase 2: select max 3 8 9 5 10 6 2 12 15 18 20

  31. Phase 2: select max 9 8 5 3 10 6 2 12 15 18 20

  32. Phase 2: select max 3 8 5 9 10 6 2 12 15 18 20

  33. Phase 2: select max 8 6 5 9 10 3 2 12 15 18 20

  34. Phase 2: select max 2 6 5 9 10 3 8 12 15 18 20

  35. Phase 2: select max 6 3 5 9 10 2 8 12 15 18 20

  36. Phase 2: select max 2 3 5 9 10 6 8 12 15 18 20

  37. Phase 2: select max 5 3 2 9 10 6 8 12 15 18 20

  38. Phase 2: select max 2 3 5 9 10 6 8 12 15 18 20

  39. Phase 2: select max 3 2 5 9 10 6 8 12 15 18 20

  40. Phase 2: select max 2 3 5 9 10 6 8 12 15 18 20

  41. Heap sort completed 2 3 5 9 10 6 8 12 15 18 20

  42. Heap sort time complexity for i  1 to n-1 do insert element s[i] into the heap consisting of the elements s[0]…s[i-1] for i  n-1 down to 1 do swap s[0] and s[i]“demote” s[0] to its proper place in the heap consisting of the elements s[0]...s[i-1] O( n log n ) O( log n ) operations O( n log n )

  43. About heap sort • Build heap phase can be improved toO( n ) if array is rearranged “bottom-up” • Overall complexity still O( n log n ) because of second phase • O( n log n ) time complexity is guaranteed • Note that heap sort is just a more clever version of selection sort since a maximum is repeatedly selected and placed in its proper position

More Related