Skip to main content

Heap Sort

Heap is a data structure that is a complete binary tree that satisfies the heap property.

Usually, we use tree to represent the heap.

The parent node is always greater (or smaller) than the child node.

In python, min heap is implemented by heapq library.

In C++, we can use priority_queue to implement the heap.

Codes

import heapq

heap = []
heapq.heappush(heap, 1)
heapq.heappush(heap, 2)
heapq.heappush(heap, 3)

print(heapq.heappop(heap))

Description

Time complexity: O(nlogn)O(n \log n)

The heap structure can be maintained by AVL tree.

Space complexity: O(n)O(n)