1 / 19

Design and Analysis of Algorithms Heap sort

Design and Analysis of Algorithms Heap sort. Haidong Xue Summer 2012, at GSU. Max-Heap. A complete binary tree, and …. every level is completely filled , except possibly the last , which is filled from left to right . Yes. No. Yes. Max-Heap.

natara
Download Presentation

Design and Analysis of Algorithms 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. Design and Analysis of AlgorithmsHeapsort HaidongXue Summer 2012, at GSU

  2. Max-Heap • A complete binary tree, and … every level is completely filled,except possibly the last, which is filled from left to right Yes No Yes

  3. Max-Heap • Satisfy max-heap property: parent >= children 16 14 10 8 7 9 3 2 Since it is a complete tree, it can be put into an array without lose its structure information.

  4. Max-Heap 16 14 10 8 7 9 3 2 1 2 3 5 7 8 4 6

  5. Max-Heap For element at i: Parent index =parent(i)= floor(i/2); Left child index = left(i)=2*i; Right child index =right(i)=2*i +1 Last non-leaf node = floor(length/2) • Use an array as a heap 1 16 2 3 14 10 4 5 6 7 8 7 9 3 8 i=3 2 floor(i/2)=floor(1.5)=1 2*i = 6 2*i+1=7 floor(length/2)=4 7 9 3 2 14 16 10 8 1 2 3 5 7 8 4 6

  6. Max-Heapify • Input: A compete binary tree rooted at i, whose left and right sub trees are max-heaps; last node index • Output: A max-heap rooted at i. • Algorithm: 1. Choose the largest node among node i, left(i), right(i) . 2. if(the largest node is not i){ • Exchange i with the largest node • Max-Heapify the affected subtree }

  7. Max-Heapify Example 2 16 10 14 7 9 3 8

  8. Heapsort for a heap • Input: a heap A • Output: a sorted array A Algorithm: 1. Last node index l = A’s last node index 2. From the last element to the second{ exchange (current, root); l--; Max-Heapify(A, root, l); }

  9. Heapsort example 16 14 10 3 9 8 7 2

  10. Heapsort example 14 8 10 3 9 2 7 16

  11. Heapsort example 10 8 9 14 3 2 7 16

  12. Heapsort example 9 8 3 14 10 2 7 16

  13. Heapsort example 8 7 3 14 10 2 9 16

  14. Heapsort example 7 2 3 14 10 8 9 16

  15. Heapsort example 3 2 7 14 10 8 9 16

  16. Array -> Max-Heap • Input: a array A • Output: a Max-Heap A Algorithm: Considering A as a complete binary tree, from the last non-leaf node to the first one{ Max-Heapify(A, current index, last index); }

  17. Build Heap Example 7 10 8 16 14 14 8 16 10 7

  18. Heapsort • Input: array A • Output: sorted array A Algorithm: 1. Build-Max-Heap(A) 2. Last node index l = A’s last node index 3. From the last element to the second{ exchange (current, root); l--; Max-Heapify(A, root, l); } Let’s try it

  19. Analysis of Heapsort • Input: array A • Output: sorted array A Algorithm: 1. Build-Max-Heap(A) 2. Last node index l = A’s last node index 3. From the last element to the second{ exchange (current, root); l--; Max-Heapify(A, root, l); } O(n) or O(nlgn) O(n) O(nlgn)

More Related