-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap1.py
More file actions
56 lines (56 loc) · 1.77 KB
/
heap1.py
File metadata and controls
56 lines (56 loc) · 1.77 KB
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
import math
# 打印树
def print_tree(array, unit_width=2):
length = len(array)
depth = math.ceil(math.log2(length+1))
index = 1
width = 2 ** depth -1
for i in range(depth):
for j in range(2**i):
print('{:^{}}'.format(array[index],width * unit_width),end=' '*unit_width)
index +=1
if index >= length:
return
width = width // 2
print()
# origin = [0, 30, 20, 80, 40, 50, 10, 60, 70, 90]
origin = [0, 30, 20, 80, 90, 50, 10, 60, 70, 40]
total = len(origin) - 1 # 初始待排元素个数, 即n
print(origin)
print_tree(origin)
def heap_adjust(n ,i, array:list):
'''
调整的结点起点在n // 2, 保证所有调整的结点都有孩子
:param n: 待比较个数
:param i: 当前结点的下标
:param array: 待排序数据
:return: None
'''
while 2 * i <= n:
lchild_index = 2 * i
max_child_index = lchild_index # n= 2i
if n > lchild_index and array[lchild_index + 1] > array[lchild_index]:
max_child_index = lchild_index + 1
if array[max_child_index] > array[i]:
array[i], array[max_child_index] = array[max_child_index], array[i]
i = max_child_index
else:
break
# print()
# heap_adjust(total, 1, origin)
# print_tree(origin)
def max_heap(total, array:list):
for i in range(total//2, 0, -1):
heap_adjust(total, i, array)
return array
# print_tree(max_heap(total,origin))
def sort(total, array:list):
while total > 1:
array[1], array[total] = array[total], array[1]
total -= 1
if total == 2 and array[total] >= array[total - 1]:
break
heap_adjust(total, 1, array)
return array
print_tree(sort(total, origin))
# print(origin)