复制代码 代码如下:
#!/usr/bin/python
import sys

def left_child(node):
return node * 2 + 1

def right_child(node):
return node * 2 + 2

def parent(node):
if (node % 2):
return (i - 1) / 2
else:
return (i - 2) / 2

def max_heapify(array, i, heap_size):
l = left_child(i)
r = right_child(i)

largest = i
if l < heap_size and array[l] > array[i]:
largest = l

if r < heap_size and array[r] > array[largest]:
largest = r

if largest != i:
array[i], array[largest] = array[largest], array[i]
max_heapify(array, largest, heap_size)

def build_max_heap(array):
for i in range(len(array) / 2, -1, -1):
max_heapify(array, i, len(array))


def heap_sort(array):
build_max_heap(array)
for i in range(len(array) - 1, 0, -1):
array[0], array[i] = array[i], array[0]
max_heapify(array, 0, i)


if __name__ == "__main__":
array = [0, 2, 6, 98, 34, -5, 23, 11, 89, 100, 7]
heap_sort(array)

for a in array:
sys.stdout.write("%d " % a)
标签:
堆排序

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com

评论“python 实现堆排序算法代码”

暂无“python 实现堆排序算法代码”评论...