The heap sort of "turn" algorithm

Source: Internet
Author: User

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
  1. To adjust a subtree with index as its root
  2. N: Number of elements in the heap
  3. int maxheap (int a[],int index,int n) {
  4. int largestindex = index;
  5. //Left Dial hand node
  6. int leftindex = 2*index;
  7. //Right child node
  8. int rightindex = 2*index+1;
  9. if (leftindex <= n && a[leftindex] > A[largestindex]) {
  10. Largestindex = Leftindex;
  11. }
  12. if (rightindex <= n && a[rightindex] > A[largestindex]) {
  13. Largestindex = Rightindex;
  14. }
  15. //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
  16. //Exchange A[index],a[largetindex], so that index and children meet the heap nature
  17. int temp;
  18. if (largestindex! = index) {
  19. //Exchange A[index],a[largetindex]
  20. temp = A[index];
  21. A[index] = A[largestindex];
  22. A[largestindex] = temp;
  23. //re-adjust the subtree with Largestindex as root
  24. Maxheap (A,largestindex,n);
  25. }
  26. return 0;
  27. }

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
  1. Build heap: Convert an array a[1-n] into a maximum heap
  2. int buildmaxheap (int a[],int n) {
  3. int i;
  4. the elements in the//sub-array a[(N/2+1,N/2+2......N)] are all leaves in the tree
  5. For (i = n/2;i >= 1;i--) {
  6. //Adjust the tree with I as the root node to make it the largest heap
  7. Maxheap (A,i,n);
  8. }
  9. return 0;
  10. }

A array

16 7 3 20 17 8

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
  1. Heap Sort
  2. int heapsort (int a[],int n) {
  3. int temp;
  4. //bulidmaxheap constructs a maximum heap for the input array
  5. Buildmaxheap (A,n);
  6. ////The largest element in the array at root a[1], it can be exchanged with a[n] to achieve the final correct position
  7. For (int i = n;i >= 2;i--) {
  8. //Exchange
  9. temp = A[i];
  10. A[i] = a[1];
  11. A[1] = temp;
  12. //a[i] has reached the correct position, removed from the heap
  13. n--;
  14. //re-adjust to maintain maximum heap properties
  15. Maxheap (A,1,n);
  16. }
  17. return 0;
  18. }



(a) (b) (c) (d)

(e) (f) (g)

(h) (i) (j) (k)

Red is the result of sorting;

Code:

[CPP]View Plaincopy
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. Adjustment heap
    4. int maxheap (int a[],int index,int n) {
    5. int largestindex = index;
    6. //Left Dial hand node
    7. int leftindex = 2*index;
    8. //Right child node
    9. int rightindex = 2*index+1;
    10. if (leftindex <= n && a[leftindex] > A[largestindex]) {
    11. Largestindex = Leftindex;
    12. }
    13. if (rightindex <= n && a[rightindex] > A[largestindex]) {
    14. Largestindex = Rightindex;
    15. }
    16. //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
    17. //Exchange A[index],a[largetindex], so that index and children meet the heap nature
    18. int temp;
    19. if (largestindex! = index) {
    20. //Exchange A[index],a[largetindex]
    21. temp = A[index];
    22. A[index] = A[largestindex];
    23. A[largestindex] = temp;
    24. //re-adjust the subtree with Largestindex as root
    25. Maxheap (A,largestindex,n);
    26. }
    27. return 0;
    28. }
    29. Build heap: Convert an array a[1-n] into a maximum heap
    30. int buildmaxheap (int a[],int n) {
    31. int i;
    32. the elements in the//sub-array a[(N/2+1,N/2+2......N)] are all leaves in the tree
    33. For (i = n/2;i >= 1;i--) {
    34. //Adjust the tree with I as the root node to make it the largest heap
    35. Maxheap (A,i,n);
    36. }
    37. return 0;
    38. }
    39. Heap Sort
    40. int heapsort (int a[],int n) {
    41. int temp;
    42. //bulidmaxheap constructs a maximum heap for the input array
    43. Buildmaxheap (A,n);
    44. ////The largest element in the array at root a[1], it can be exchanged with a[n] to achieve the final correct position
    45. For (int i = n;i >= 2;i--) {
    46. //Exchange
    47. temp = A[i];
    48. A[i] = a[1];
    49. A[1] = temp;
    50. //a[i] has reached the correct position, removed from the heap
    51. n--;
    52. //re-adjust to maintain maximum heap properties
    53. Maxheap (A,1,n);
    54. }
    55. return 0;
    56. }
    57. int main () {
    58. int n = 6;
    59. //a[0] No, the root node of the heap is starting from 1.
    60. int a[] = {0,3,17,8,7,16,20};
    61. Heapsort (A,n);
    62. For (int i = 1;i <= n;i++) {
    63. printf ("%d", a[i]);
    64. }
    65. return 0;
    66. }

The heap sort of "turn" algorithm

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.