--heap-sort heap sequencing of algorithm analysis

Source: Internet
Author: User

Heap sorting is a sort of in-place sorting algorithm that does not use extra array space and runs at O (NLGN). In this article, we will introduce the implementation of heap sequencing.
To understand heap sequencing, let's begin by understanding a concept, a completely binary tree. A heap is either a complete binary tree or an approximate complete binary tree. What is a complete binary tree? Baidu Encyclopedia gives the definition: complete binary tree: In addition to the last layer, the number of nodes on each layer reaches the maximum; On the last layer, there are only a few junctions on the right. The following two small graphs are used to illustrate the complete binary tree and the non-complete two-fork tree. (Pictures from Baidu, we can ignore the watermark ...)

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 (both the largest heap or the smallest 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.

Heap sorting is also based on the idea of divided treatment, the main three steps are as follows:

    1. initialization, which starts from the first non-leaf node, so that the tree with its root is Dagen;
    2. Swaps the top element of the heap with the heap tail element, filtering out the maximum value. Adjust the new heap to the large root heap.
    3. Repeat 2, each time you filter out the largest element in the heap, the heap sort is complete.
      This time, we have an array of examples as follows: array length length=10;

      The corresponding heap structure:

Starting from the first non-leaf node, build the initial heap.

void BUILD_MAX_HEAP(int A[],intlength){    int i;    for(i=((length/2)-1);i>=0;i--)//length/2-1,为第一个非叶结点        MAX_HEAPIFY(A,i);}

Keep the nature of the heap

void Max_heapify (intA[],int i) {int l,r,largest,middle;L=left (i);R=right (i);    if(LA[l]>A[i]) largest = L;    Elselargest= I;    if(RA[r]>A[Largest]) Largest = R;    if(largest!=i) {middle=A[Largest];        A[largest]=AI;        A[I]=middle;Max_heapify (A, largest);}}

Concrete implementation of heap sorting

length){    BUILD_MAX_HEAP(A,length);    int i,middle;    for(i=length-1;i>0;i--)    {        middle=A[0];        A[0]=A[i];        A[i]=middle;        heap_size--;        MAX_HEAPIFY(A,0);    }   }

The following is a simple procedure performed by the program, and the analysis is not comprehensive enough, but sufficient to illustrate the problem.

1. Analyze the For loop in step 4, Build_max_heap (A,length), which is the process of building the initial heap. I loop from length/2-1 to 0, that is, from 4 cycles to 0. (4 is the first non-leaf node)
(1) i=4;max_heapify (a,i); max_heapify (a,4);
<1> calculates the number of the left leaf node l=left (i) = (2*i+1) = 9; Calculates the number of the right leaf node r=right (i) = (2*i+2) = 10;
Note: When calculating the number of left and right leaf nodes here, it is important to note whether the array starts from 0 or 1, and if starting from 0, the left leaf node is (2*i+1) and the leaf node is (2*i+2); If starting from 1, the Zuo is 2*i; the right leaf is 2*i+1

<2> determine the size of the left and right leaf nodes and the root node, assigning the larger value of the node number to largest;
Heap_size is the size of the heap and begins heap_size=length=9

ifA[l]>A[i])        largest = l;    else         largest= i;    ifA[r]>A[largest])        largest = r;

When I=4, largest=4

<3> determines if largest is equal to the root node, and if it is not the root node, it indicates that the left or right leaf node is larger than the value of the root node, then the value of the root node and the largest node are exchanged.

if(largest!=i)    {        middle=A[largest];        A[largest]=A[i];        A[i]=middle;        MAX_HEAPIFY(A,largest);    }

Because this is largest=i here, do not perform this step and perform the next for loop

(2) i=3; Max_heapify (a,i); max_heapify (a,3);
<1> calculates the number of the left leaf node l=left (i) = (2*i+1) = 7; Calculates the number of the right leaf node r=right (i) = (2*i+2) = 8;

<2> determine the size of the left and right leaf nodes and the root node, assigning the larger value of the node number to largest;
Largest=7

<3> determines if largest is equal to the root node, and if it is not the root node, it indicates that the left or right leaf node is larger than the value of the root node, then the value of the root node and the largest node are exchanged.

<4> executive Max_heapify (a,largest); Max_heapify (a,7); Adjust the tree with largest roots to a large root heap

(3) i=2; the steps are the same as the <1>~<4> in 2. Largest=6, an exchange occurs. Not analyzed here.

(4) I=1; Time analysis process reference steps and 2 in <1>~<4>. Results after the execution

(5) I=0;

2. The For loop analysis in step 6, that is, the process of filtering out the maximum value, reducing the size of the heap, and preserving the nature of the heap.
Length=10,i from length-1 to 1, i.e. from 9 cycles to 1

for(i=length-1;i>0;i--)    {        middle=A[0];        A[0]=A[i];        A[i]=middle;        heap_size--;        MAX_HEAPIFY(A,0);    }   

(1) i=9; Exchange A[i] and a[0], at which point I is the last element of the heap, A[0] is the top element of the heap, the largest element, swapping the largest element to the end of the heap. And the scale of the heap is reduced by one, that is, the heap to be reordered is the red-boxed part, at which time the max_heapify (a,0) is executed;

(2) i=8; Exchange A[i] and a[0];heap_size–; Max_heapify (a,0);

(3) The following loop is no longer an example, and we can see that the largest element in the current heap is filtered out each time.

3. Finally gives the program to run:

Program source code: Heap Sort Implementation code

--heap-sort heap sequencing of algorithm analysis

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.