260 likes | 449 Views
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.
E N D
CSCI 6212 Design and Analysis of AlgorithmsHeapsort • Dr. Juman Byun • The George Washington University
(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 10 2 4 1
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
Max-Heap in Array 16 14 10 8 7 9 3 2 4 1
16 Max-Heapify(A,i) 4 10 14 7 9 3 2 8 1
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)
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)
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)
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)
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)
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)
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)
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)
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
16 Heapsort(A) 4 10 14 7 9 3 Build-Max-Heap(A) 2 8 1
16 Heapsort(A) 14 10 8 7 9 3 Build-Max-Heap(A) 2 4 1
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
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
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
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
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
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 )
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 )