10. Heapsort and Priority Queues

Heaps

  1. A binary heap is a nearly complete binary tree without child-parent pointers, usually stored as an array.
  2. Each node (array element) contains a value (key) and perhaps data associated with the key.
  3. The tree is completely filled on all levels except possibly the lowest (bottom) level, which is filled from the left(Array indexes are assigned top to bottom, left to right).

Two types of heaps: the max-heap and the min-heap

For a max-heap: every node other than the root has a key less than or equal to the key in its parent.

For a min-heap: every node other than the root has a key greater than or equal to the key in its parent.

Basic properties of a max-heap:

  1. The key in the root is the largest key.
  2. Given any node in a max-heap, the node and all its descendants form a max-heap, (i.e., the subtree rooted at the node is a maximum heap, too).
10. Heapsort and Priority Queues_第1张图片

Heaps operation

我们常用两个方式来操作:

  1. Swap up. If a key is greater than the key of its parent, swap them.
  2. Swap down. If a key is less than the key of one of its two children (or a child), swap the key with the larger key of the two children.

MAX-HEAPIFY operation

Make the subtree rooted at i into a max-heap, assuming that the subtrees rooted at LEFT(i) and RIGHT(i) are max-heaps already.
Method: swap down.

10. Heapsort and Priority Queues_第2张图片
4E7BD4B4-5058-46F5-8FD7-1528DE6C75F2.png

Time required: O(logn).
The height of an element is the distance(logn) to the bottom level.
即 O(height)

BUILD-MAX-HEAP operation

Given an unordered array A, turn it into a max-heap.

The idea: fix one element at a time, working from the leaves up to the root. As each element’s turn comes, its children are already fixed.

时间复杂度为O(n)。

10. Heapsort and Priority Queues_第3张图片
过程

Heapsort

Method: Turn the array into a max-heap. Then repeatedly remove the maximum element of the heap into the proper place in the array.

Time required: <= O(n) + (n - 1)*O(logn) = O(nlogn)


10. Heapsort and Priority Queues_第4张图片
10. Heapsort and Priority Queues_第5张图片
过程

Priority Queues

A priority queue is a data structure for maintaining a set S of elements, each with an associated value called the key.

A max-priority queue supports the following operations.

  1. INSERT(S, x) inserts the element x into the set S, i.e., S ← S ∪ {x}.

  2. MAXIMUM(S) returns the element of S with the largest key.

  3. EXTRACT-MAX(S) removes and returns the element of S with the largest key.
    过程:在写code时,先将root(a[0])和lastnode(a[last]) exchange, 输出最后一个node,然后执行 heapsize - 1。最后对新的heap进行排位(比较children,取大的值和root替换)。
    原因:我们用数组实现heap。

  4. INCREASE-KEY(S,x,k) increases the value of element x’s key to the new value k, which is assumed to be at least as large as x’s current key value.

We can implement the operations of a max-priority queue S, using a max-heap A.

  1. HEAP-MAXIMUM(A) ---O(1)
  2. HEAP-EXTRACT-MAX(A) --- O(logn) using MAX-HEAPIFY(A, 1)
  3. HEAP-INCREASE-KEY(A, i, key) --- O(logn)
  4. MAX-HEAP-INSERT(A, key) --- O(logn) using HEAP-INCREASE-KEY(A,heap size[A],key)

总结

Heapsort is an in place sorting algorithm that runs in O(nlogn).

  1. It achieves asymptotically optimal running time
  2. It only takes a constant amount of space outside the input array
  3. The heapsort algorithm uses the heap data structure, which can also be used for minimum/maximum priority queues
  4. It is an optimal sorting algorithm

你可能感兴趣的:(10. Heapsort and Priority Queues)