[硕.Love Python] HeapSort(堆排序)
来源:互联网 发布:seo全网优化指南 pdf 编辑:程序博客网 时间:2024/05/30 23:42
def adjust(a, root, n):
k = a[root-1]
c = root * 2
while c <= n:
if c + 1 <= n and a[c] > a[c-1]:
c += 1
if a[c-1] <= k:
break
a[c/2-1] = a[c-1]
c *= 2
a[c/2-1] = k
def heapSort(a):
n = len(a)
for i in xrange(n / 2, 0, -1):
adjust(a, i, n)
for i in xrange(n - 1, 0, -1):
a[0], a[i] = a[i], a[0]
adjust(a, 1, i)
if __name__ == '__main__':
from random import shuffle
data = range(100)
shuffle(data)
print data
heapSort(data)
k = a[root-1]
c = root * 2
while c <= n:
if c + 1 <= n and a[c] > a[c-1]:
c += 1
if a[c-1] <= k:
break
a[c/2-1] = a[c-1]
c *= 2
a[c/2-1] = k
def heapSort(a):
n = len(a)
for i in xrange(n / 2, 0, -1):
adjust(a, i, n)
for i in xrange(n - 1, 0, -1):
a[0], a[i] = a[i], a[0]
adjust(a, 1, i)
if __name__ == '__main__':
from random import shuffle
data = range(100)
shuffle(data)
print data
heapSort(data)
print data
0 0
- [硕.Love Python] HeapSort(堆排序)
- 堆排序(HeapSort)
- 堆排序 heapSort
- HeapSort 堆排序
- C++ 堆排序 (HeapSort)
- 堆排序heapsort
- 堆排序(HeapSort)
- HeapSort(堆排序 C++)
- 堆排序(Heapsort)
- 堆排序 (Heapsort)
- 堆排序 Heapsort
- 堆排序(heapsort)
- 堆排序(HeapSort)
- 堆排序(heapsort)
- 堆排序(heapsort)
- heapsort堆排序
- 堆排序heapsort
- 堆排序 HeapSort
- iOS使用imageIO完成渐进加载图片
- htaccess 实现URL重写
- Python安装后pip不能用的问题
- BZOJ 1059, 矩阵游戏
- (斯坦福机器学习课程笔记)正则化和机器学习应用的建议
- [硕.Love Python] HeapSort(堆排序)
- javaWeb文件上传报文分析
- Ijkplayer->001初步调用
- 基础练习 杨辉三角形
- MYSQL 查询表字段,数据库,字段详情
- file /usr/java/jdkxx/bin/java from install of jdkxx conflict解决方法
- OpenCL 开发配置
- AFO
- 树链剖分