Python堆排序

本文阅读 0 分钟
首页 Python笔记 正文
  1. def heapSort(alist):
  2. if alist == None or len(alist) == 0:
  3. return
  4. length = len(alist)
  5. output = []
  6. for i in range(length):
  7. tempLen = len(alist)
  8. for j in range(tempLen//2-1, -1, -1):
  9. preIndex = j
  10. preVal, heap = alist[preIndex], False
  11. while 2 * preIndex < tempLen - 1 and not heap:
  12. curIndex = 2 * preIndex + 1
  13. if curIndex < tempLen - 1:
  14. if alist[curIndex] < alist[curIndex+1]:
  15. curIndex += 1
  16. if preVal >= alist[curIndex]:
  17. heap = True
  18. else:
  19. alist[preIndex] = alist[curIndex]
  20. preIndex = curIndex
  21. alist[preIndex] = preVal
  22. output.insert(0, alist.pop(0))
  23. return output
  24. test = [2, 6, 5, 9, 10, 3, 7]
  25. print(heapSort(test))
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

Python分治算法
« 上一篇 01-21
Python下使用正则表达式
下一篇 » 01-21

发表评论

惪特博客
  • 文章总数:
    18496 篇
  • 评论总数:
    53283 条
  • 标签总数:
    8869 个
  • 总浏览量:
    22006898 次
  • 最后更新:
    2天前

最多点赞

随便看看

标签TAG