Heap Sort)

Source: Internet
Author: User

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 (,) meet the requirements of heap properties (1) and (2) respectively)

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 a N-2 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, these comparison operations are repeated during the next sorting.
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
③ Because 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 re-set R [1 .. in n-1], the record R [1] with the largest keyword is exchanged with the last record R [n-1] between the region, 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 set R [1 .. n-2] adjusted 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 except R [1] may violate the heap nature, the subtree with any other node as the root is 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, you must exchange the keyword between R [low] and the two child nodes, that is, R [low] and R [large] (R [large]. key = max (R [2low]. key, R [2low + 1]. key. After switching, the node R [large] may violate the heap nature. Because the two Subtrees of the node (if any) are still heap, the above adjustment process can be repeated, adjust the tree with R [large] as the root. This process ends until the currently adjusted node meets the heap nature, or the 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 the node numbers are leaves, so the subtree with these nodes as the root is already a heap. In this way, we only need to sequentially set the sequence number to-1 ,..., You can change the node 1 as the root subtree to heap.
For more information about the algorithm, see tutorial ].

5. Sorting of Big Top heaps


Void heapsort (elem r [], int N )...{
// Perform heap sorting on the record sequence R [1. N.
For (I = n/2; I> 0; -- I)
// Build R [1. N] into a large top heap
Heapadjust (R, I, n );
For (I = N; I> 1; -- I )...{
R [1] records → R;
// Record the heap top and the unsorted subsequence
// Exchange the last record in R [1. I]
Heapadjust (R, 1, I-1 );
// Reset the R [1 .. I-1] to the Big Top heap
}
} // Heapsort
The filtering algorithm is as follows. In order to change R [s .. m] To "Big Top Heap", "filter" in the algorithm should be performed down the child node with a large keyword.
Void heapadjust (elem r [], int S, int m )...{
// It is known that the key record in R [s .. m] is deleted by R [s]. Key
// The outer part meets the definition of the heap. This function adjusts the R [s ].
// Key word to make R [S. M] A Big Top heap (
// Keyword of the record)
Rc = R [s];
For (j = 2 * s; j <= m; j * = 2)... {// filter down child nodes with large keys
If (j <M & R [J]. Key <R [J + 1]. Key) ++ J;
If (RC. Key> = R [J]. Key) break; // RC should be inserted on location S.
R [s] = R [J]; S = J;
}
R [s] = RC; // insert
} // Heapadjust

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.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.