-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHeapSort.py
More file actions
45 lines (32 loc) · 1.04 KB
/
HeapSort.py
File metadata and controls
45 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Python program for implementation of heap Sort
# To heapify subtree rooted at index i.
# n is size of heap
def heapify(arr, n, root):
largest = root
left = 2 * root + 1
right = 2 * root + 2
# See if left child of root exists and is greater than root
if left < n and arr[root] < arr[left]:
largest = left
# See if right child of root exists and is greater than root
if right < n and arr[largest] < arr[right]:
largest = right
# Change root, if needed
if largest != root:
arr[root], arr[largest] = arr[largest], arr[root]
# Heapify the root.
heapify(arr, n, largest)
# The main function to sort an array of given size
def heap_sort(arr):
n = len(arr)
# Build a maxheap.
for i in range(n, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
# Driver code to test above
if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
heap_sort(arr)
print("Sorted array:", arr)