Heap sort and quick sort, merging sort are all the common sorts of time complexity O (N*LOGN). Before learning heap sorting, first explain what is the data structure of the two-fork heap.
The definition of a binary heap
The two-fork heap is a complete binary tree or a nearly complete binary tree.
The two-fork heap satisfies two characteristics:
1. The key value of a parent node is always greater than or equal to (less than or equal to) the key value of any child node.
2. The Saozi right subtree of each node is a binary heap (both the maximum heap and the smallest heap).
The maximum heap when the key value of a parent node is always greater than or equal to the key value of any one child node. The minimum heap when the key value of a parent node is always less than or equal to the key value of any one child node. The following illustration shows a minimum heap:
As a result of several other heaps (two-item heap, Fibonacci Ponachi, etc.) used less, generally two-fork heap is referred to as the heap.
Storage of the heap
Generally, arrays are used to represent the heap, and the parent node of the I node is labeled (i–1)/2. Its left and right sub nodes are labeled 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node left and right sub-node subscript 1 and 2 respectively.
Heap operation--Insert Delete
The following first given the "data structure C + + language description" of the smallest heap of building insert Delete diagram, and then give me the implementation code, it is best to see the map before you look at the code.