Skip to content

Commit d0d434d

Browse files
authored
Update 07.堆排序.py
1 parent 7428065 commit d0d434d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

十大排序算法/07.堆排序.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,25 @@ def max_heapify(nums, start, end):
3232
else:
3333
break
3434

35+
36+
# 最小堆调整。子节点较小者与父节点交换
37+
def min_heapify(nums, start, end):
38+
root = start
39+
while True:
40+
child = 2*root+1
41+
if child > end:
42+
break
43+
# 获取较小子节点
44+
if child+1 <= end and nums[child] > nums[child+1]:
45+
child += 1
46+
# 较小子节点成为父节点
47+
if nums[child] < nums[root]:
48+
nums[child], nums[root] = nums[root], nums[child]
49+
root = child
50+
else:
51+
break
3552

53+
3654
if __name__ == '__main__':
3755
nums = [2, 1, 6, 7, 9, 5, 0, 4, 3, 8]
38-
print(heap_sort(nums))
56+
print(heap_sort(nums))

0 commit comments

Comments
 (0)