Skip to content
Code ReadingSorting with a Heap
Read the Big-O

Sorting with a Heap

It pushes n numbers onto a heap and pops them back one by one. Each push and pop is O(log n): the total?
1import heapq
2
3def heap_sort(values):
4 heap = []
5 for v in values:
6 heapq.heappush(heap, v)
7 return [heapq.heappop(heap) for _ in range(len(heap))]