heap sortheapsort algorithm heapsort(a): #create max heap build_max_heap from unordered array a #...
TRANSCRIPT
![Page 1: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/1.jpg)
Heap Sort
![Page 2: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/2.jpg)
Heapsort AlgorithmHeapsort(A):
#Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size)
Max-heapify(A, 1) because new root may violate max heap property
![Page 3: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/3.jpg)
Build Max HeapBuild_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
![Page 4: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/4.jpg)
Heap ■ The root of the tree is A[1], and given the index i of a node, we can
easily compute the indices of its parent, left child, and right child: def parent(i):
return i/2 def left(i): try: return 2*i except: pass def right(i): try: return 2 *i + 1 except: pass
![Page 5: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/5.jpg)
Max-Heapifydef max_heapify(arr,i):
n = len(arr) l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr
![Page 6: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/6.jpg)
Start with an array (it is not a max heap)
6
10 1
4 7
2 8 11
9 3
6 10 1 4 7 9 3 2 8 11
![Page 7: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/7.jpg)
Exchange 7 and 11
6
10 1
4 7 9 3
2 8 11
6 10 1 4 7 9 3 2 8 11j
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
def max_heapify(arr,i): n = len(arr)
l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr
def parent(i): return i/2 def left(i): try: return 2*i except: pass def right(i): try: return 2 *i + 1 except: pass
![Page 8: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/8.jpg)
Exchange 4 and 8
6
10 1
4 11 9 3
2 8 7
6 10 1 4 11 9 3 2 8 7j
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
![Page 9: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/9.jpg)
Exchange 9 and 1
6
10 1
8 11 9 3
2 4 7
6 10 1 8 11 9 3 2 4 7
j
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
![Page 10: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/10.jpg)
Exchange 10 and 11
6
10 9
8 11 1 3
2 4 7
6 10 9 8 11 1 3 2 4 7
j
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
![Page 11: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/11.jpg)
Exchange 6 and 11
6
11 9
8 10 1 3
2 4 7
6 11 9 8 10 1 3 2 4 7
j
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
def max_heapify(arr,i): n = len(arr)
l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr
![Page 12: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/12.jpg)
Exchange 6 and 10
11
6 9
8 10 1 3
2 4 7
11 6 9 8 10 1 3 2 4 7
j
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
i largest
def max_heapify(arr,i): n = len(arr)
l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr
![Page 13: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/13.jpg)
Exchange 6 and 7
11
10 9
8 6 1 3
2 4 7
11 10 9 8 6 1 3 2 4 7
j i
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
largest
def max_heapify(arr,i): n = len(arr)
l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr
![Page 14: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/14.jpg)
max_heapify
11
10 9
8 7 1 3
2 4 6
11 10 9 8 7 1 3 2 4 6
j i
Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
largest
def max_heapify(arr,i): n = len(arr)
l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr
![Page 15: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/15.jpg)
Sorting
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size)
Max-heapify(A,1) because new root may violate max heap property
![Page 16: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/16.jpg)
Exchange 11 and 6
11
10 9
8 7 1 3
2 4 6
11 10 9 8 7 1 3 2 4 6
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 17: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/17.jpg)
Remove 11 from the heap
6
10 9
8 7 1 3
2 4 11
6 10 9 8 7 1 3 2 4 11
![Page 18: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/18.jpg)
Swap 6 and 10
6
10 9
8 7 1 3
2 4 1111
6 10 9 8 7 1 3 2 4 11
![Page 19: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/19.jpg)
Exchange 6 and 8
10
6 9
8 7 1 3
2 4 1111
10 6 9 8 7 1 3 2 4 11
![Page 20: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/20.jpg)
Exchange 10 and 4
10
8 9
6 7 1 3
2 4 1111
10 8 9 6 7 1 3 2 4 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 21: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/21.jpg)
Remove 10 from the heap
4
8 9
6 7 1 3
2 10 1111
4 8 9 6 7 1 3 2 10 11
![Page 22: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/22.jpg)
Exchange 4 and 9
4
8 9
6 7 1 3
2 1110
4 8 9 6 7 1 3 2 10 11
![Page 23: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/23.jpg)
Exchange 9 and 2
9
8 4
6 7 1 3
2 1110
9 8 4 6 7 1 3 2 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 24: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/24.jpg)
Remove 9 from the heap
2
8 4
6 7 1 3
9 1110
2 8 4 6 7 1 3 9 10 11
![Page 25: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/25.jpg)
Exchange 2 and 8
2
8 4
6 7 1 3
11109
2 8 4 6 7 1 3 9 10 11
![Page 26: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/26.jpg)
Exchange 2 and 7
8
2 4
6 7 1 3
11109
8 2 4 6 7 1 3 9 10 11
![Page 27: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/27.jpg)
Exchange 8 and 3
8
7 4
6 2 1 3
11109
8 7 4 6 2 1 3 9 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 28: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/28.jpg)
Remove 8 from the heap
3
7 4
6 2 1 8
11109
3 7 4 6 2 1 8 9 10 11
![Page 29: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/29.jpg)
Exchange 3 and 7
3
7 4
6 2 1
11109
8
3 7 4 6 2 1 8 9 10 11
![Page 30: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/30.jpg)
Exchange 3 and 6
7
3 4
6 2 1
11109
8
7 3 4 6 2 1 8 9 10 11
![Page 31: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/31.jpg)
Exchange 1 and 7
7
6 4
3 2 1
11109
8
7 6 4 3 2 1 8 9 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 32: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/32.jpg)
Remove 7 from the heap
1
6 4
3 2 7
11109
8
1 6 4 3 2 7 8 9 10 11
![Page 33: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/33.jpg)
Exchange 1 and 6
1
6 4
3 2
11109
87
1 6 4 3 2 7 8 9 10 11
![Page 34: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/34.jpg)
Exchange 1 and 3
6
1 4
3 2
11109
87
6 1 4 3 2 7 8 9 10 11
![Page 35: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/35.jpg)
Exchange 6 and 2 and remove from the heap
6
3 4
1 2
11109
87
6 3 4 1 2 7 8 9 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 36: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/36.jpg)
Exchange 4 and 2
2
3 4
1
11109
876
2 3 4 1 6 7 8 9 10 11
![Page 37: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/37.jpg)
Exchange 4 and 1
4
3 2
1
11109
876
4 3 2 1 6 7 8 9 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 38: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/38.jpg)
Remove 4, exchange 1 and 3
1
3 2
4
11109
876
1 3 2 4 6 7 8 9 10 11
![Page 39: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/39.jpg)
Exchange 2 and 3, and remove 3 from heap
3
1 2
11109
8764
3 1 2 4 6 7 8 9 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 40: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/40.jpg)
Exchange 1 and 2 and remove from heap
2
1
11109
8764
3
2 1 3 4 6 7 8 9 10 11
Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property
![Page 41: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/41.jpg)
The array is sorted
1
3
11109
8764
2
1 2 3 4 6 7 8 9 10 11
![Page 42: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/42.jpg)
Sorted Output6 10 1 4 7 9 3 2 8 11
Hea
p So
rt Al
gorit
hm (b
uild
+ s
ort)
1 2 3 4 6 7 8 9 10 11
11 10 9 8 7 1 3 2 4 6
Build Heap (Max)
Sort Max Heap
![Page 43: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/43.jpg)
Heapsort AlgorithmHeapsort(A):
#Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2
exchange A[1] with A[i] discard node i from heap (decrement heap size)
Max-heapify(A, 1) because new root may violate max heap property
![Page 44: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/44.jpg)
Build Max HeapBuild_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)
![Page 45: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/45.jpg)
Heap ■ The root of the tree is A[1], and given the index i of a node, we can
easily compute the indices of its parent, left child, and right child: def parent(i):
return i/2 def left(i): try: return 2*i except: pass def right(i): try: return 2 *i + 1 except: pass
![Page 46: Heap SortHeapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2 exchange A[1] with A[i] discard node](https://reader034.vdocuments.mx/reader034/viewer/2022051607/60374fd394b6a67656354f9b/html5/thumbnails/46.jpg)
Max-Heapifydef max_heapify(arr,i):
n = len(arr) l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr