2018-10-19 12:48:28 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2019-08-19 13:37:49 +00:00
|
|
|
# This heap class start from here.
|
2018-10-19 12:48:28 +00:00
|
|
|
class Heap:
|
2019-10-05 05:14:13 +00:00
|
|
|
def __init__(self): # Default constructor of heap class.
|
|
|
|
self.h = []
|
|
|
|
self.currsize = 0
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def leftChild(self, i):
|
|
|
|
if 2 * i + 1 < self.currsize:
|
|
|
|
return 2 * i + 1
|
|
|
|
return None
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def rightChild(self, i):
|
|
|
|
if 2 * i + 2 < self.currsize:
|
|
|
|
return 2 * i + 2
|
|
|
|
return None
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def maxHeapify(self, node):
|
|
|
|
if node < self.currsize:
|
|
|
|
m = node
|
|
|
|
lc = self.leftChild(node)
|
|
|
|
rc = self.rightChild(node)
|
|
|
|
if lc is not None and self.h[lc] > self.h[m]:
|
|
|
|
m = lc
|
|
|
|
if rc is not None and self.h[rc] > self.h[m]:
|
|
|
|
m = rc
|
|
|
|
if m != node:
|
|
|
|
temp = self.h[node]
|
|
|
|
self.h[node] = self.h[m]
|
|
|
|
self.h[m] = temp
|
|
|
|
self.maxHeapify(m)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def buildHeap(
|
|
|
|
self, a
|
|
|
|
): # This function is used to build the heap from the data container 'a'.
|
|
|
|
self.currsize = len(a)
|
|
|
|
self.h = list(a)
|
|
|
|
for i in range(self.currsize // 2, -1, -1):
|
|
|
|
self.maxHeapify(i)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def getMax(self): # This function is used to get maximum value from the heap.
|
|
|
|
if self.currsize >= 1:
|
|
|
|
me = self.h[0]
|
|
|
|
temp = self.h[0]
|
|
|
|
self.h[0] = self.h[self.currsize - 1]
|
|
|
|
self.h[self.currsize - 1] = temp
|
|
|
|
self.currsize -= 1
|
|
|
|
self.maxHeapify(0)
|
|
|
|
return me
|
|
|
|
return None
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def heapSort(self): # This function is used to sort the heap.
|
|
|
|
size = self.currsize
|
|
|
|
while self.currsize - 1 >= 0:
|
|
|
|
temp = self.h[0]
|
|
|
|
self.h[0] = self.h[self.currsize - 1]
|
|
|
|
self.h[self.currsize - 1] = temp
|
|
|
|
self.currsize -= 1
|
|
|
|
self.maxHeapify(0)
|
|
|
|
self.currsize = size
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def insert(self, data): # This function is used to insert data in the heap.
|
|
|
|
self.h.append(data)
|
|
|
|
curr = self.currsize
|
|
|
|
self.currsize += 1
|
|
|
|
while self.h[curr] > self.h[curr / 2]:
|
|
|
|
temp = self.h[curr / 2]
|
|
|
|
self.h[curr / 2] = self.h[curr]
|
|
|
|
self.h[curr] = temp
|
|
|
|
curr = curr / 2
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def display(self): # This function is used to print the heap.
|
|
|
|
print(self.h)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def main():
|
|
|
|
l = list(map(int, input().split()))
|
|
|
|
h = Heap()
|
|
|
|
h.buildHeap(l)
|
|
|
|
h.heapSort()
|
|
|
|
h.display()
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|