forked from JimChengLin/JimCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.py
94 lines (59 loc) · 2.06 KB
/
HeapSort.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
87
88
89
90
91
92
93
94
from numbers import Number
from typing import List
sample = [1, 4, 1, 6, 9, 0, 2]
def heapify(root: List[Number], parent_node=0):
def left(index):
return index * 2 if index != 0 else 1
def right(index):
return left(index) + 1
left_node = left(parent_node)
right_node = right(parent_node)
nodes = [node for node in (parent_node, left_node, right_node) if node < len(root)]
max_node = max(nodes, key=lambda node: root[node])
if max_node != parent_node:
root[parent_node], root[max_node] = root[max_node], root[parent_node]
heapify(root, max_node)
def build(root: List[Number]):
for i in reversed(range(len(root) // 2)):
heapify(root, i)
def heap_sort(root: List[Number]) -> List[Number]:
result = []
build(root)
while root:
heapify(root)
result.insert(0, root.pop(0))
return result
print(heap_sort(sample))
sample = [1, 4, 1, 6, 9, 0, 2]
build(sample)
def get_max(priority_q: List[Number]):
return priority_q[0]
print(get_max(sample))
def pop_max(priority_q: List[Number]):
result = priority_q.pop(0)
heapify(priority_q)
return result
print(pop_max(sample))
def set_value(priority_q: List[Number], current_node: int, value: Number):
def parent(node):
return node - (2 - (node % 2))
original_value = priority_q[current_node]
priority_q[current_node] = value
if original_value < value:
while current_node != 0:
parent_node = parent(current_node)
if priority_q[parent_node] < priority_q[current_node]:
priority_q[parent_node], priority_q[current_node] = priority_q[current_node], priority_q[parent_node]
current_node = parent_node
else:
break
elif original_value > value:
heapify(priority_q, current_node)
print(sample)
set_value(sample, 2, 10)
print(sample)
def insert(priority_q: List[Number], value: Number):
priority_q.append(float('-inf'))
set_value(priority_q, len(priority_q) - 1, value)
insert(sample, 14)
print(sample)