堆和堆排序
堆有两种: max-heap 和 min-heap. Max-heap 一般用来排序,Min-heap 用来实现 priority queue.
max-heap的定义是:for each i: A[parent(i)] >= A[i]
min-heap: for each i: A[parent(i)] <= A[i]
无论要实现哪种,都会用到 Left(i), Right(i), Parent(i).
Heap-sort() 要用到的操作有:
Build-max-heap(): build a heap from an unsorted array
Max-heapify(): maintain the max-heap property
Priority queue 支持的操作有:
max-priority queue:
min-priority queue 支持的操作有:insert(), minimum(), extract-min(), decrease-key()