-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path130_heapify.py
More file actions
30 lines (28 loc) · 831 Bytes
/
130_heapify.py
File metadata and controls
30 lines (28 loc) · 831 Bytes
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
class Solution:
"""
@param: A: Given an integer array
@return: nothing
"""
def heapify(self, A):
# start from mid-depth to sift down
for i in range(len(A) // 2, -1, -1):
self.siftdown(A, i)
def siftdown(self, A, i):
"""
sift down
1. pick the smaller child to swap
2. if parent is already small than both children, no need to continue
3. continue to sift down in next depth
"""
n = len(A)
while i * 2 + 1 < n:
# left child
_i = i * 2 + 1
if _i + 1 < n and A[_i + 1] < A[_i]:
# right child
_i += 1
if A[_i] >= A[i]:
# if its already steady
break
A[i], A[_i] = A[_i], A[i]
i = _i