-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathheap_sort.py
More file actions
42 lines (37 loc) · 1.03 KB
/
heap_sort.py
File metadata and controls
42 lines (37 loc) · 1.03 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
# max heap
def heapify(ar, n, i):
maxIdx = i
leftChild, rightChild = 2 * i + 1, 2 * i + 2
# left child greater than root
if leftChild < n and ar[leftChild] > ar[maxIdx]:
maxIdx = leftChild
# right child greater than root
if rightChild < n and ar[rightChild] > ar[maxIdx]:
maxIdx = rightChild
#swap and heapify until max ele not found
if maxIdx != i:
ar[i], ar[maxIdx] = ar[maxIdx], ar[i]
heapify(ar, n, maxIdx)
def heapSort(ar):
n = len(ar)
# max heapify
for i in range(n // 2 - 1, -1, -1):
heapify(ar, n, i)
# heap sort
for i in range(n - 1, 0, -1):
ar[i], ar[0] = ar[0], ar[i]
heapify(ar, i, 0)
# main method
def main():
# input code
n = int(input("Enter array size : "))
print("Enter array elements :", end=" ")
ar = [int(i) for i in input().split()][:n]
# function call
heapSort(ar)
# display sorted array
print("Sorted Array :", end=" ")
for i in ar:
print(i, end=" ")
# main method call
main()