30 lines
505 B
ReStructuredText
30 lines
505 B
ReStructuredText
>>> import heapq
|
|
|
|
|
|
>>> heap = [1, 3, 5, 7, 2, 4, 3]
|
|
>>> heapq.heapify(heap)
|
|
>>> heap
|
|
[1, 2, 3, 7, 3, 4, 5]
|
|
|
|
>>> while heap:
|
|
... heapq.heappop(heap), heap
|
|
(1, [2, 3, 3, 7, 5, 4])
|
|
(2, [3, 3, 4, 7, 5])
|
|
(3, [3, 5, 4, 7])
|
|
(3, [4, 5, 7])
|
|
(4, [5, 7])
|
|
(5, [7])
|
|
(7, [])
|
|
|
|
|
|
>>> def heapsort(iterable):
|
|
... heap = []
|
|
... for value in iterable:
|
|
... heapq.heappush(heap, value)
|
|
...
|
|
... while heap:
|
|
... yield heapq.heappop(heap)
|
|
>>> list(heapsort([1, 3, 5, 2, 4, 1]))
|
|
[1, 1, 2, 3, 4, 5]
|
|
|