Skip to content

Commit 7650bb0

Browse files
committed
Day 40: implement heapify in time
1 parent a84000e commit 7650bb0

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

day38/README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ push - takes an integer as an argument and adds it into the heap, returning
6262
nothing. Should complete in `O(logn)` time.
6363

6464
heapify - takes an array of integers and forms a heap out of them. Should run in
65-
`O(nlogn)` (?) time.
65+
`O(n)` time.
6666

6767
size - takes no arguments and returns the number of integers in the heap `n`
6868
in `O(1)` time.
@@ -83,8 +83,20 @@ efficient way to maintain that ordering even when elements are being deleted
8383
and added. The tree structure of the heap allows you to find elements in
8484
`O(logn)` time rather than `O(n)` time like in a normal array or linked list.
8585

86+
---
87+
8688
Some more thinking around heapify, aka just cram an unsorted array into
87-
a binary tree, and then compare and swap unordered parent-children pairs:
89+
a binary tree, and then compare and swap unordered parent-children pairs,
90+
from the bottom up:
91+
92+
arr = [7, 6, 5, 4, 3, 2, 1]
93+
94+
1
95+
/ \
96+
3 2
97+
/ \ / \
98+
4 6 7 5
99+
88100

89101
arr = [4, 5, 7, 3, 9, 6, 1]
90102

@@ -134,8 +146,19 @@ arr[index] = 4
134146

135147
done.
136148

137-
Need to do one comparison set for each parent node, and then done. So O(n) time!
149+
Need to do one bubbleDown each parent node from the bottom up, and then done.
150+
On the bottom-most level, `n/2` nodes can move at most 0 levels down.
151+
Next level, `n/4` nodes can move at most 1 level down.
152+
etc.
153+
154+
So it's a summation on i from 0 to logn:
138155

156+
`n/(2**i) * i`
139157

158+
which turns out to be `O(n)` work!
140159

160+
Edit: fix `heapify` code.
141161

162+
For more information:
163+
- https://www.cs.umd.edu/~meesh/351/mount/lectures/lect14-heapsort-analysis-part.pdf
164+
- https://www.youtube.com/watch?v=MiyLo8adrWw

day38/heap.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def __init__(self, arr=[]):
55
self.heap = deque()
66
self.size = 0
77
if len(arr) > 0:
8+
self.size = len(arr)
89
self.heapify(arr)
910

1011
# runtime: O(logn) aka the height of the heap
@@ -14,32 +15,33 @@ def getMin(self):
1415
self.size -= 1
1516
if self.size > 0:
1617
self.heap.appendleft(self.heap.pop())
17-
self.bubbleDown()
18+
self.bubbleDown(0)
1819
return ret
1920

2021
# runtime: O(1)
2122
def peek(self):
2223
if self.size > 0:
2324
return self.heap[0]
2425

25-
# runtime: O(logn) aka the height of the tree
26+
# runtime: O(logn) aka the height of the heap
2627
def push(self, val):
2728
self.size += 1
2829
self.heap.append(val)
2930
self.bubbleUp()
3031

3132
# runtime: O(nlogn)
3233
def heapify(self, arr):
33-
for item in arr:
34-
self.push(item)
34+
self.heap = deque(arr)
35+
for i in xrange(self.size-1, -1, -1):
36+
self.bubbleDown(i)
3537

3638
# runtime: O(1)
3739
def isEmpty(self):
3840
return self.size == 0
3941

40-
def bubbleDown(self):
42+
def bubbleDown(self, index):
4143
if self.size > 0:
42-
i = 0
44+
i = index
4345
h = self.heap
4446
withinBounds = 2*i + 2 < self.size
4547
while withinBounds and (h[i] > h[2*i + 1] or h[i] > h[2*i + 2]):

0 commit comments

Comments
 (0)