-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_heap.py
executable file
·54 lines (39 loc) · 1.21 KB
/
build_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
# Convert Array Into Heap
# Author: jerrybelmonte
# Input: First line contains an integer n.
# Second line contains n integers separated by a space.
# Output: First line contains the total number of swaps.
# The following lines contain the swap operations on the array.
def build_heap(data):
"""Build a heap from ``data`` inplace.
Returns a sequence of swaps performed by the algorithm.
"""
swaps = []
n = len(data)
parent = lambda i: (i - 1) // 2
left_child = lambda i: 2 * i + 1
right_child = lambda i: 2 * i + 2
min_index = lambda i, j: j if j < n and data[j] < data[i] else i
def heapify(i):
ndx = i
left = left_child(i)
right = right_child(i)
ndx = min_index(ndx, left)
ndx = min_index(ndx, right)
if i != ndx:
swaps.append((i,ndx))
data[i], data[ndx] = data[ndx], data[i]
heapify(ndx)
for i in range((n//2), -1, -1):
heapify(i)
return swaps
def main():
n = int(input())
data = list(map(int, input().split()))
assert len(data) == n
swaps = build_heap(data)
print(len(swaps))
for i, j in swaps:
print(i, j)
if __name__ == "__main__":
main()