Heap sorting
1. Definition of heap sorting
N key word sequences KL, K2 ,..., Kn is called a heap, and only when the sequence meets the following properties (referred to as heap properties ):
(1) KI ≤ k2i and Ki ≤ k2i + 1
Or (2) KI ≥ k2i and Ki ≥ k2i + 1 (1 ≤ I ≤
)
If we store the vector R [1 .. n] as a full binary tree storage structure, the heap is essentially a Complete Binary Tree that meets the following requirements: the keywords of any non-leaf node in the tree are not greater than (or not less) keywords of the left and right children (if any) nodes.
[Example] the keyword sequences (,) and (,) satisfy the heap properties (1) and (2) respectively, so they are all heap, the corresponding full binary tree is shown in the example of the small root heap and the example of the large root heap.
2. Large and small heaps
The keyword of the root node (also known as the heap top) is the heap of the smallest node keyword in the heap.
The keyword of the root node (also known as the heap top) is the publisher of all node keywords in the heap, known as the Big root heap.
Note:
① Any subtree In the heap is also a heap.
② The heap discussed above is actually binary heap, which can be defined similarly.
3. Heap sorting features
Heapsort is a tree-based sorting method.
The characteristic of heap sorting is that during the sorting process, R [L .. n] As a Complete Binary Tree ordered storage structure, using the inherent relationship between the parent and child nodes in the Complete Binary Tree [see the binary tree ordered storage structure ], select the record with the maximum or minimum keyword in the unordered area.
4. Differences between heap sorting and direct insert sorting
Directly select the sort, In order .. n] to select the record with the smallest keyword. The record must be compared n-1 times, and then in R [2 .. n] in the selection of the minimum keyword record, and need to do N-2 times
Comparison. In fact, many comparisons in the next N-2 comparison may have been done in the previous n-1 comparison, but since these comparison results were not retained in the previous sort, therefore, the execution is repeated in the next sorting.
These comparison operations are performed.
Partial comparison results can be saved in a tree structure to reduce the number of comparisons.
5. Heap sorting
Heap sorting utilizes the largest (or least) keyword of the top record of a large root heap (or a small root heap) so that the largest (or least) keyword is selected in the current unordered area) keyword record becomes simple.
(1) the basic idea of sorting with big roots
① First build the initial file R [1. N] into a large root heap, which is the initial unordered Zone
② Then exchange the record R [1] (heap top) with the last record R [N] In the unordered zone, and obtain the new unordered zone R [1 .. n-1] And the ordered zone R [N], and meet the requirements of R [1 .. n-1]. keys ≤ r [N]. key
③
Since the new root R [1] After the swap may violate the heap nature, the R [1 .. n-1] of the unordered zone should be changed to the heap. Then, record R [1] with the largest keyword in R [1 .. n-1] and the region
The last record R [n-1] is exchanged, and a new unordered zone R [1 .. n-2] and ordered zone R [n-1 .. n], and still satisfies the relationship R [1 .. n-
2]. Keys ≤ r [n-1... n]. Keys, also adjust R [1 .. N-2] to heap.
......
Until there is only one element in the unordered area.
(2) Basic operations on the Sorting Algorithm of the big root heap:
① Initialization operation: Create R [1. N] as the initial heap;
② Basic operations for sorting each trip: swap the top record R [1] of the unordered zone with the last record in the interval, then adjust the new unordered area to the heap (also weigh and build the heap ).
Note:
① The large n-1 keywords can be selected to sort the files in ascending order.
② Sorting with a small root heap is similar to using a large root heap, but the sorting result is descending and ordered. Opposite to Direct selection: at any time, unordered areas in heap sorting are always before the ordered areas, the ordered area gradually expands from the back to the end of the original vector to the end of the whole vector.
(3) Heap Sorting Algorithm:
Void heapsort (seqiast R)
{// Perform heap sorting on R [1. N]. Use R [0] as the temporary storage unit.
Int I;
Buildheap (r); // build R [1-N] into the initial heap
For (I = N; I> 1; I --) {// perform heap sorting on the current unordered zone R [1. I] for a total of N-1 troughs.
R [0] = R [1]; R [1] = R [I]; R [I] = R [0]; // swap the last record in the heap and heap
Heapify (R, 1, I-1); // retuned R [1 .. I-1] to heap, only R [1] may violate the heap nature
} // Endfor
} // Heapsort
(4) Implementation of buildheap and heapify Functions
Because the operation to adjust the heap must be used to construct the initial heap, the implementation of heapify is discussed first.
① Heapify function ideology and Methods
R [L .. i] is a heap with R [1] as the root. After R [1] is exchanged with R [I], the new unordered zone R [1 .. in I-1], only the value of R [1] has changed, so
R [1] may violate the heap nature. The subtree with any other node as the root is a heap. Therefore, when the range to be adjusted is R [low... high], you only need to adjust the tree with R [low] as the root.
"Adjust Heap"
The left and right subtree (if any) of R [low] are already heap, the root R [2low] and R [2low + 1] of the two Subtrees are the largest nodes of the keywords in their Subtrees. If
R [low]. if the key is not less than the keywords of the two child nodes, R [low] does not violate the heap nature. The tree with R [low] as the root is already heap and does not need to be adjusted; otherwise, R [low] and
The two child nodes with large keywords are exchanged, that is, R [low] and R [large]
(R [large]. Key = max (R [2low]. Key, R [2low + 1]. Key) exchange. After switching, the node R [large] may be in violation of the heap nature.
As the two Subtrees of the node (if any) are still heap, repeat the above adjustment process to adjust the tree with R [large] as the root. This process until the currently adjusted node has satisfied the heap nature,
Or this node is already a leaf. The above process is like filtering through a sieve. The smaller keywords are screened layer by layer, and the larger keywords are selected layer by layer. Therefore, someone calls this method "Limit Method ".
Specific algorithms [see tutorial materials]
② Buildheap implementation
To adjust the initial file R [L. N] to a large root heap, the Child tree corresponding to the Complete Binary Tree must be changed to a heap with each node as the root.
Obviously, the tree with only one node is a heap, and in the Complete Binary Tree, all serial numbers
The nodes are all leaves, so the subtree rooted in these nodes is already heap. In this way, we only need
,
-1 ,..., You can change the node 1 as the root subtree to heap.
For more information about the algorithm, see tutorial ].
5. Sorting instances of large root heaps
For keyword sequences (,), see [animation demonstration] for the change of the Complete Binary Tree and Its Storage Structure During Heap creation ].
6. Algorithm Analysis
The heapify time is mainly composed of the time overhead of establishing the initial heap and re-building the heap. Both of them are implemented by calling heapify.
The worst time complexity of heap sorting is O (nlgn ). The average performance of heap sorting is closer to the worst performance.
Because the initial heap requires a large number of comparisons, the heap sorting is not suitable for files with a small number of records.
Heap sorting is local sorting, and the auxiliary space is O (1 ),
It is an unstable sorting method.