栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

了解如何在Python中创建堆

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

了解如何在Python中创建堆

这是在这里找到的代码的略微修改版本:http : //pre.activestate.com/recipes/577086-heap-
sort/

def HeapSort(A,T):    def heapify(A):        start = (len(A) - 2) / 2        while start >= 0: siftDown(A, start, len(A) - 1) start -= 1    def siftDown(A, start, end):        root = start        while root * 2 + 1 <= end: child = root * 2 + 1 if child + 1 <= end and T.count(A[child]) < T.count(A[child + 1]):     child += 1 if child <= end and T.count(A[root]) < T.count(A[child]):     A[root], A[child] = A[child], A[root]     root = child else:     return    heapify(A)    end = len(A) - 1    while end > 0:        A[end], A[0] = A[0], A[end]        siftDown(A, 0, end - 1)        end -= 1if __name__ == '__main__':    text = "the quick brown fox jumped over the the quick brown quick log log"    heap = list(set(text.split()))    print heap    HeapSort(heap,text)    print heap

输出量

['brown', 'log', 'jumped', 'over', 'fox', 'quick', 'the']['jumped', 'fox', 'over', 'brown', 'log', 'the', 'quick']

您可以在此处将该程序可视化 http://goo.gl/2a9Bh



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号