-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort_Q10.py
More file actions
59 lines (47 loc) · 1.63 KB
/
HeapSort_Q10.py
File metadata and controls
59 lines (47 loc) · 1.63 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ucse20057 | Gayatri Rout | DSA assignment
# swapping function
def swap(ary, x, y):
ary[x], ary[y] = ary[y], ary[x]
# Heapify function: maintaining max-heap
def heapify(ary,size, i): # i = non-leaf node
# initializing left & right child nodes
left = 2*i
right = 2*i + 1
# setting root as max value
largest = i
# compare root with left child to find max amongst them
if ((left < size) and ary[left] > ary[i] ):
largest = left
# compare current largest with right child to find max amongst them
if ((right < size) and ary[right] > ary[largest] ):
largest = right
# changing the root and maintaining heap property
if largest != i:
swap(ary, i, largest)
heapify(ary, size, largest)
# BuildHeap function: building max-heap
def buildHeap(ary):
n = size//2 # first non-leaf node
for i in range(n, -1, -1):
heapify(ary, size, i)
# HeapSort: finally sorting the array
def heapSort(ary):
buildHeap(ary) # max heap
# deleting elements one by one
for j in range(size-1, 0, -1):
swap(ary, j, 0)
heapify(ary, j, 0)
# Obtaining the array to be sorted
ary = []
size = int(input("Size of array? "))
for i in range(1, size+1):
ary.append(int(input("Enter element {0}: " .format(i))))
print("\nOriginal array:", end=" ")
print(ary)
if size == 1 or size == 0:
print("\nSorted array:", end=" ") # one element array is already sorted
print(ary)
else:
heapSort(ary)
print("\nSorted array:", end=" ")
print(ary)