1 / 26

CSCI 6212 Design and Analysis of Algorithms Heapsort

CSCI 6212 Design and Analysis of Algorithms Heapsort. Dr. Juman Byun The George Washington University. Space Complexity. Complete Binary Tree. (Binary) Max-Heap. 16. 14. 10. 8. 7. 9. 3. 2. 4. 1. (Binary) Max-Heap. 1. 16. 2. 3. 14. 10. 4. 5. 6. 7. 8. 7. 9. 3. 8. 9.

liv
Download Presentation

CSCI 6212 Design and Analysis of Algorithms 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. CSCI 6212 Design and Analysis of AlgorithmsHeapsort • Dr. Juman Byun • The George Washington University

  2. Space Complexity

  3. Complete Binary Tree

  4. (Binary) Max-Heap 16 14 10 8 7 9 3 2 4 1

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

  6. To get array indices of relative positions • Parent(i) • 1 return floor(i/2) • Left(i) • 1 return 2i • Right(i) • 1 return 2i + 1

  7. Max-Heap in Array 16 14 10 8 7 9 3 2 4 1

  8. 16 Max-Heapify(A,i) 4 10 14 7 9 3 2 8 1

  9. 16 4 10 14 7 9 3 2 8 1 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  10. Max-Heapify(A,2) 16 1 4 10 2 3 14 7 9 3 4 5 6 7 2 8 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  11. Max-Heapify(A,2) 16 1 4 10 2 3 14 7 9 3 4 5 6 7 2 8 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  12. Max-Heapify(A,2) 16 1 4 10 2 3 14 7 9 3 4 5 6 7 2 8 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  13. Max-Heapify(A,2) 16 1 14 10 2 3 4 7 9 3 4 5 6 7 2 8 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  14. Max-Heapify(A,4) 16 1 14 10 2 3 4 7 9 3 4 5 6 7 2 8 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  15. Max-Heapify(A,4) 16 1 14 10 2 3 8 7 9 3 4 5 6 7 2 4 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  16. Max-Heapify(A,9) 16 1 14 10 2 3 8 7 9 3 4 5 6 7 2 4 1 10 8 9 Max-Heapify(A,i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l <= A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r <= A.heap-size andA[r] > A[largest] 7 largest = r 8 if largest != i 9 exchange A[i] with A[largest] 10 Max-Heapfiy(A, largest)

  17. Build-Max-Heap(A) • 1 A.heap-size = A.length • 2 for i = floor(A.length/2) downto 1 • 3 Max-Heapify(A,i) 16 1 14 10 2 3 8 7 9 3 4 5 6 7 2 4 1 10 8 9

  18. 16 Heapsort(A) 4 10 14 7 9 3 Build-Max-Heap(A) 2 8 1

  19. 16 Heapsort(A) 14 10 8 7 9 3 Build-Max-Heap(A) 2 4 1

  20. 16 Heapsort(A) 14 10 8 7 9 3 Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] 2 4 1 1 2 3 4 5 6 7 8 9 10

  21. Heapsort(A) Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] 16 1 14 10 2 3 8 7 9 3 4 5 6 7 2 4 1 8 9 10

  22. Heapsort(A) Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] 1 1 14 10 2 3 8 7 9 3 4 5 6 7 2 4 16 8 9 10

  23. Heapsort(A) Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] A.heap-size = A.heap-size - 1 1 1 14 10 2 3 8 7 9 3 4 5 6 7 2 4 16 8 9 10

  24. Heapsort(A) Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] A.heap-size = A.heap-size - 1 Max-Heapify(A,1) 14 1 8 10 2 3 4 7 9 3 4 5 6 7 2 1 16 8 9 10

  25. Running Time of Heapsort(A) Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] A.heap-size = A.heap-size - 1 Max-Heapify(A,1) O( lg n )

  26. Running Time of Heapsort(A) Build-Max-Heap(A) for i = A.length downto 2 exchange A[1] with A[i] A.heap-size = A.heap-size - 1 Max-Heapify(A,1) O( n lg n )

More Related