注意: 此文中的二叉堆默认为最小二叉堆
此处的堆排序是降序排序
# coding:utf-8
__author__ = 'taohao'
"""
heap sort include delete min , build a binary heap, insert
assume the heap is the min-heap that means the root node is the min node
"""
def insert_heap(heap, insert_element):
"""
insert_heap is the same as the insert sort algorithm
but here, the insert_sort function is no business with the heap_sort
插入操作是上滤操作,和下面的下滤相对应
:param heap: the binary heap
:param insert_element: the element needed to be inserted
:return:
"""
length = len(heap)
heap[length] = insert_element
tem = insert_element
child = length
parent = (child - 1)/2
while parent >= 0:
if heap[child] < heap[parent]:
heap[child] = heap[parent]
child = parent
parent = (child - 1)/2
else:
heap[child] = tem
break
def delete_min_heap(heap):
"""
delete the min node in the heap
attention to the 'heap[0] = heap[length-1]'
:param heap:
:return:
"""
length = len(heap)
if length == 0:
raise
min_node = heap[0]
heap[0] = heap[length-1] # put the last one to heap[0] for the next
# call percolate_down_heap(heap, 0)
del heap[length-1]
percolate_down_heap(heap, 0, length-1)
print min_node
def percolate_down_heap(heap, hole_node, length):
"""
the most important function in handling heap
percolate down the heap for delete the min element of the heap
also when build a new binary heap , this function is needed. recursion to build heap
下滤关系到两个操作:
1:删除最小的元素,也就是在堆排序的时候,需要将最小的节点移到最后节点,这时需要重新建堆的堆的长度会减少1
2:在创建堆的时候需要从最后的叶子节点向上循环下滤,特别要注意这一点,是不断地向上进行下滤操作
:param heap:
:param hole_node: the empty node
:param length: the length of the heap
:return:
"""
child_left = hole_node * 2 + 1
child_right = child_left + 1 # when the length of the heap is even number,
# there is no right child
# length = len(heap)
tem = heap[hole_node] # use the last node to insert and rebuild the heap
# when the root node(the min node) is delete
while child_left < length:
small_node = child_left
if child_right < length and heap[child_right] < heap[child_left]:
small_node = child_right
if tem < heap[small_node]:
break
heap[hole_node] = heap[small_node]
hole_node = small_node
child_left = hole_node * 2 + 1
child_right = child_left + 1
heap[hole_node] = tem
def build_heap(array):
"""
use the percolate_down_heap(heap, hole_node) function to build the heap
:param array:
:return:
"""
length = len(array)
parent = (length - 2)/2
while parent >= 0:
percolate_down_heap(array, parent, length)
parent -= 1
def heap_sort(array):
"""
use one array space to sort the array
put the min node to the last node of the heap every time
and the length of the heap that needed to sorted should be minus 1
:param array:
:return:
"""
build_heap(array)
i = len(array) - 1
while i > 0:
tem = array[0] # exchange the min node and the last node
# so the length in the 'percolate_down_heap(array, 0, i)' should -1
array[0] = array[i]
array[i] = tem
percolate_down_heap(array, 0, i)
i -= 1
if __name__ == '__main__':
array = [19, 3, 6, 99, 80, 39, 4, 2, 27, 50, 6, 75, 201, 98, 28, 91]
build_heap(array)
print array
heap_sort(array)
print array