Skip to content

Commit 7341b76

Browse files
committed
python中的heapq模块即堆排序
1 parent efe7f5f commit 7341b76

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

heapq.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#python中的堆排序peapq模块
2+
3+
heapq模块实现了python中的堆排序,并提供了有关方法。让用Python实现排序算法有了简单快捷的方式。
4+
5+
heapq的官方文档和源码:[8.4.heapq-Heap queue algorithm](https://docs.python.org/2/library/heapq.html)
6+
7+
下面通过举例的方式说明heapq的应用方法
8+
9+
##实现堆排序
10+
11+
#! /usr/bin/evn python
12+
#coding:utf-8
13+
14+
from heapq import *
15+
16+
def heapsort(iterable):
17+
h = []
18+
for value in iterable:
19+
heappush(h,value)
20+
return [heappop(h) for i in range(len(h))]
21+
22+
if __name__=="__main__":
23+
print heapsort([1,3,5,9,2])
24+
25+
###heappush()
26+
27+
heapq.heappush(heap, item):将item压入到堆数组heap中。如果不进行此步操作,后面的heappop()失效
28+
29+
###heappop()
30+
31+
heapq.heappop(heap):从堆数组heap中取出最小的值,并返回。
32+
33+
>>> h=[] #定义一个list
34+
>>> from heapq import * #引入heapq模块
35+
>>> h
36+
[]
37+
>>> heappush(h,5) #向堆中依次增加数值
38+
>>> heappush(h,2)
39+
>>> heappush(h,3)
40+
>>> heappush(h,9)
41+
>>> h #h的值
42+
[2, 5, 3, 9]
43+
>>> heappop(h) #从h中删除最小的,并返回该值
44+
2
45+
>>> h
46+
[3, 5, 9]
47+
>>> h.append(1) #注意,如果不是压入堆中,而是通过append追加一个数值
48+
>>> h #堆的函数并不能操作这个增加的数值,或者说它堆对来讲是不存在的
49+
[3, 5, 9, 1]
50+
>>> heappop(h) #从h中能够找到的最小值是3,而不是1
51+
3
52+
>>> heappush(h,2) #这时,不仅将2压入到堆内,而且1也进入了堆。
53+
>>> h
54+
[1, 2, 9, 5]
55+
>>> heappop(h) #操作对象已经包含了1
56+
1
57+
58+
###heapq.heappushpop(heap, item)
59+
60+
是上述heappush和heappop的合体,同时完成两者的功能.注意:相当于先操作了heappush(heap,item),然后操作heappop(heap)
61+
62+
>>> h
63+
[1, 2, 9, 5]
64+
>>> heappop(h)
65+
1
66+
>>> heappushpop(h,4) #增加4同时删除最小值2并返回该最小值,与下列操作等同:
67+
2 #heappush(h,4),heappop(h)
68+
>>> h
69+
[4, 5, 9]
70+
71+
###heapq.heapify(x)
72+
73+
x必须是list,此函数将list变成堆,实时操作。从而能够在任何情况下使用堆的函数。
74+
75+
>>> a=[3,6,1]
76+
>>> heapify(a) #将a变成堆之后,可以对其操作
77+
>>> heappop(a)
78+
1
79+
>>> b=[4,2,5] #b不是堆,如果对其进行操作,显示结果如下
80+
>>> heappop(b) #按照顺序,删除第一个数值并返回,不会从中挑选出最小的
81+
4
82+
>>> heapify(b) #变成堆之后,再操作
83+
>>> heappop(b)
84+
2
85+
86+
###heapq.heapreplace(heap, item)
87+
88+
是heappop(heap)和heappush(heap,item)的联合操作。注意,与heappushpop(heap,item)的区别在于,顺序不同,这里是先进行删除,后压入堆
89+
90+
>>> a=[]
91+
>>> heapreplace(a,3) #如果list空,则报错
92+
Traceback (most recent call last):
93+
File "<stdin>", line 1, in <module>
94+
IndexError: index out of range
95+
>>> heappush(a,3)
96+
>>> a
97+
[3]
98+
>>> heapreplace(a,2) #先执行删除(heappop(a)->3),再执行加入(heappush(a,2))
99+
3
100+
>>> a
101+
[2]
102+
>>> heappush(a,5)
103+
>>> heappush(a,9)
104+
>>> heappush(a,4)
105+
>>> a
106+
[2, 4, 9, 5]
107+
>>> heapreplace(a,6) #先从堆a中找出最小值并返回,然后加入6
108+
2
109+
>>> a
110+
[4, 5, 9, 6]
111+
>>> heapreplace(a,1) #1是后来加入的,在1加入之前,a中的最小值是4
112+
4
113+
>>> a
114+
[1, 5, 9, 6]
115+
116+
###heapq.merge(\*iterables)
117+
118+
举例:
119+
120+
>>> a=[2,4,6]
121+
>>> b=[1,3,5]
122+
>>> c=merge(a,b)
123+
>>> list(c)
124+
[1, 2, 3, 4, 5, 6]
125+
126+
[归并排序](https://github.com/qiwsir/algorithm/blob/master/merge_sort.md)中详细演示了本函数的使用方法。
127+
128+
###heapq.nlargest(n, iterable[, key]),heapq.nsmallest(n, iterable[, key])
129+
130+
获取列表中最大、最小的几个值。
131+
132+
>>> a
133+
[2, 4, 6]
134+
>>> nlargest(2,a)
135+
[6, 4]
136+

0 commit comments

Comments
 (0)