Algorithm note-sorting series-heap sorting

Source: Internet
Author: User

1. Brief Introduction

Assume that the array to be sorted is int array [] and the array length is n.
It mainly utilizes the nature of the heap. Use the largest heap for ascending sorting.
First, build a heap and use the recursive root order traversal method. By exchanging elements, ensure that the root element is larger than the child element.
The array [0] on the top of the heap is exchanged with array [n-1] to ensure that the value of array [n-1] is correct. The heap is updated based on the new value of array [0.
The array [0] on top of the heap is exchanged with array [N-2] to ensure that the value of array [N-2] is correct and the heap is updated based on the new value of array [0.
···

N-1-1 comment indicates that the array [0] on the top of the heap is exchanged with array [1] to ensure that the value of array [1] is correct. The heap is updated based on the new value of array [0.

2. Complexity

The average time complexity is O (N * logN), and the space complexity is O (1 ).

3. Code

Void make_heap (int array [], int n, int node) {// build the heap
Int left = 2 * node + 1;
Int right = 2 * node + 2;
If (left> N-1) return;
Else if (right> N-1) {// The heap is a complete binary tree, so recursion is not required.
If (array [node] <array [left]) {
Swap (array [node], array [left]);
}
}
Else {
Make_heap (array, n, left );
Make_heap (array, n, right );
If (array [node] <array [left] & array [right] <= array [left]) {
Swap (array [node], array [left]);
}
Else if (array [node] <array [right] & array [left] <= array [right]) {
Swap (array [node], array [right]);
}
}
}
Void update_heap (INT array [], int N, int node) {// top-down, update heap
Int left = 2 * n + 1;
Int right = 2 * n + 2;
If (left> N-1) return;
Else if (Right> N-1 ){
If (array [node] <array [left])
Swap (array [node], array [left]);
}
Else {
If (array [node] <array [left] & array [right] <= array [left]) {
Swap (array [node], array [left]);
Update_heap (array, N, left );
}
Else if (array [node] <array [right] & array [left] <= array [right]) {
Swap (array [node], array [right]);
Update_heap (array, n, right );
}
}
}
Void heap_sort (int array [], int n ){
Make_heap (array, n, 0 );
For (int I = n; I> = 1; I --){
Swap (array [I], array [0]);
Update_heap (array, n, node );
}
}

In fact, both heap construction and update can be implemented in a non-recursive manner. For heap construction, you must first find the last node with children, array [k]. then update array [k] to array [0], where k = n/2. The k method is as follows: Assume that k exists, 2 * k + 1 = n or 2 * k + 2 = n. In the first case, k = n/2, in the second case, k = n/2-1. It is simpler to update the heap. You only need to select a path from array [0] and update it down until there are no children.
It is worth noting that for the array whose subscript starts from 0, the child nodes of node k are 2 * k + 1 and 2 * k + 2 respectively. For the subscript array starting from 1, the child nodes of node k are 2 * k and 2 * k + 1 respectively.
Heap sorting refers to selecting and sorting. In fact, the data structure of the maximum heap is used to select the largest element among the remaining elements and switch to the appropriate position.

4. References

Wikipedia-heap sorting http://en.wikipedia.org/wiki/Heapsort

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.