-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.py
86 lines (66 loc) · 2.35 KB
/
heap.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Heap(object):
HEAP_SIZE = 10
# Constructor
def __init__(self):
self.heap = [0]*Heap.HEAP_SIZE
self.currentPosition = -1
def insert(self, item):
if self.isFull():
print("Heap is full...")
return
self.currentPosition += 1 # shift current position
self.heap[self.currentPosition] = item # put item to new position
self.fixUp(self.currentPosition)
def fixUp(self, index):
parentIndex = int((index-1)/2)
while parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:
temp = self.heap[index]
self.heap[index] = self.heap[parentIndex]
self.heap[parentIndex] = temp
index = parentIndex
parentIndex = int((index-1)/2)
def isFull(self):
if self.currentPosition == Heap.HEAP_SIZE:
return True
else:
return False
# After replacement of elements during the sorting
def fixDown(self, index, upto):
while index <= upto:
leftChild = 2*index + 1
rightChild = 2*index + 2
if leftChild <= upto:
childToSwap = None
if rightChild > upto:
childToSwap = leftChild
else:
if self.heap[leftChild] > self.heap[rightChild]:
childToSwap = leftChild
else:
childToSwap = rightChild
if self.heap[index] < self.heap[childToSwap]:
temp = self.heap[index]
self.heap[index] = self.heap[childToSwap]
self.heap[childToSwap] = temp
else:
break
index = childToSwap
else:
break
# Perform O(N*logN) sorting IN PLACE!
def heapsort(self):
for i in range(0, self.currentPosition + 1):
temp = self.heap[0] # maximum value in the root
print("{0}".format(temp))
self.heap[0] = self.heap[self.currentPosition - i]
self.heap[self.currentPosition - i] = temp
self.fixDown(0, self.currentPosition - i - 1)
# Testing
heap = Heap()
heap.insert(1)
heap.insert(71)
heap.insert(81)
heap.insert(-33)
heap.insert(4)
heap.insert(22)
heap.heapsort()