http://blog.csdn.net/SJF0115/article/details/8610201
Pre-order:
(binary) A heap data structure is an array object that can be treated as a complete binary tree. Each node in the tree corresponds to the element that holds the value of the node in the array.
Each layer of the tree is filled, except for the last layer.
The root of the tree is a[1] (here is starting from 1, also can start from 0), given a node subscript I, the parent node is I/2, the left two children is 2*i, the right son is 2*i+1.
The binary stack satisfies two characteristics:
1. The key value of the parent node is always greater than or equal to (less than or equal to) the key value of any one of the child nodes.
2. The Saozi right subtree of each node is a binary heap (maximum heap or minimum heap).
The maximum heap when the parent node's key value is always greater than or equal to the key value of any one of the child nodes.
The minimum heap when the key value of the parent node is always less than or equal to the key value of any one of the child nodes.
Preserve the nature of the heap:
Maxheap is the most important subroutine that operates on the largest heap.
Sub-tree with I as root:
At each step of the algorithm, from A[i], a[left (i)], a[right (i)] finds the maximum value and has its subscript in Largestindex. If A[i] is the largest, then the subtree with the root of I is already the largest heap, and the program ends.
Otherwise I have the largest element in a sub-node to Exchange A[i],a[largetindex], so that I and children to meet the heap nature. The largestindex of the node after the exchange of the value of A[i], with the node as the root of the subtree is likely to violate the nature of the maximum heap, and therefore also recursively call the maxheap of the subtree, re-balance the sub-tree.
[CPP]View Plaincopy
- To adjust a subtree with index as its root
- N: Number of elements in the heap
- int maxheap (int a[],int index,int n) {
- int largestindex = index;
- //Left Dial hand node
- int leftindex = 2*index;
- //Right child node
- int rightindex = 2*index+1;
- if (leftindex <= n && a[leftindex] > A[largestindex]) {
- Largestindex = Leftindex;
- }
- if (rightindex <= n && a[rightindex] > A[largestindex]) {
- Largestindex = Rightindex;
- }
- //If A[INDEX] is the largest, then the subtree with index root is already the largest heap or the child node of index has the largest element
- //Exchange A[index],a[largetindex], so that index and children meet the heap nature
- int temp;
- if (largestindex! = index) {
- //Exchange A[index],a[largetindex]
- temp = A[index];
- A[index] = A[largestindex];
- A[largestindex] = temp;
- //re-adjust the subtree with Largestindex as root
- Maxheap (A,largestindex,n);
- }
- return 0;
- }
Build heap:
We can use Maxheap to make an array a[1-n] into a maximum heap, the elements in the sub-array a[n/2+1,........ n] are leaves in the tree, so each can be seen as a heap with only one element, which satisfies the requirements of the maximum heap, without adjustment. So just resize the subtree to be the root of the element in a[n/2........1] to make it the largest heap.
[CPP]View Plaincopy
- Build heap: Convert an array a[1-n] into a maximum heap
- int buildmaxheap (int a[],int n) {
- int i;
- the elements in the//sub-array a[(N/2+1,N/2+2......N)] are all leaves in the tree
- For (i = n/2;i >= 1;i--) {
- //Adjust the tree with I as the root node to make it the largest heap
- Maxheap (A,i,n);
- }
- return 0;
- }
A array
Initial heap:
Bottom-up adjustment starts from the last non-leaf node:
(a) (b) (c) (d)
Each adjustment is from the parent node, the left child node, the right child node three of the largest selection of the parent node to Exchange (after the Exchange may cause the exchange of the child node does not satisfy the nature of the heap, so after each exchange to re-exchange the child node to adjust).
Heap Sort:
At the beginning, the heap sort is first constructed into a maximum heap with buildmaxheap input array a[1-n]. And because the largest element in the array is at root a[1], it can be exchanged with a[n] to achieve the final correct position.
Now, if you "remove" the node n from the heap (not really deleted, but by modifying the number of elements in the heap n), you can easily build a[1-(n-1) to the maximum heap. The original root of the child is still the largest heap, the two new exchange of the root element is likely to violate the nature of the maximum heap. Then call Maxheap to readjust. The maximum heap is constructed in a[1-(n-1). Heap sorting repeats this process, and the size of the heap is reduced from n-1 to 2. To complete the sorting function
[CPP]View Plaincopy
- Heap Sort
- int heapsort (int a[],int n) {
- int temp;
- //bulidmaxheap constructs a maximum heap for the input array
- Buildmaxheap (A,n);
- ////The largest element in the array at root a[1], it can be exchanged with a[n] to achieve the final correct position
- For (int i = n;i >= 2;i--) {
- //Exchange
- temp = A[i];
- A[i] = a[1];
- A[1] = temp;
- //a[i] has reached the correct position, removed from the heap
- n--;
- //re-adjust to maintain maximum heap properties
- Maxheap (A,1,n);
- }
- return 0;
- }
(a) (b) (c) (d)
(e) (f) (g)
(h) (i) (j) (k)
Red is the result of sorting;
Code:
[CPP]View Plaincopy
- #include <stdio.h>
- #include <stdlib.h>
- Adjustment heap
- int maxheap (int a[],int index,int n) {
- int largestindex = index;
- //Left Dial hand node
- int leftindex = 2*index;
- //Right child node
- int rightindex = 2*index+1;
- if (leftindex <= n && a[leftindex] > A[largestindex]) {
- Largestindex = Leftindex;
- }
- if (rightindex <= n && a[rightindex] > A[largestindex]) {
- Largestindex = Rightindex;
- }
- //If A[INDEX] is the largest, then the subtree with index root is already the largest heap or the child node of index has the largest element
- //Exchange A[index],a[largetindex], so that index and children meet the heap nature
- int temp;
- if (largestindex! = index) {
- //Exchange A[index],a[largetindex]
- temp = A[index];
- A[index] = A[largestindex];
- A[largestindex] = temp;
- //re-adjust the subtree with Largestindex as root
- Maxheap (A,largestindex,n);
- }
- return 0;
- }
- Build heap: Convert an array a[1-n] into a maximum heap
- int buildmaxheap (int a[],int n) {
- int i;
- the elements in the//sub-array a[(N/2+1,N/2+2......N)] are all leaves in the tree
- For (i = n/2;i >= 1;i--) {
- //Adjust the tree with I as the root node to make it the largest heap
- Maxheap (A,i,n);
- }
- return 0;
- }
- Heap Sort
- int heapsort (int a[],int n) {
- int temp;
- //bulidmaxheap constructs a maximum heap for the input array
- Buildmaxheap (A,n);
- ////The largest element in the array at root a[1], it can be exchanged with a[n] to achieve the final correct position
- For (int i = n;i >= 2;i--) {
- //Exchange
- temp = A[i];
- A[i] = a[1];
- A[1] = temp;
- //a[i] has reached the correct position, removed from the heap
- n--;
- //re-adjust to maintain maximum heap properties
- Maxheap (A,1,n);
- }
- return 0;
- }
- int main () {
- int n = 6;
- //a[0] No, the root node of the heap is starting from 1.
- int a[] = {0,3,17,8,7,16,20};
- Heapsort (A,n);
- For (int i = 1;i <= n;i++) {
- printf ("%d", a[i]);
- }
- return 0;
- }
The heap sort of "turn" algorithm