------------------------ HEAPSORT sorting algorithm 05 (illustration)

1. heapsort

  Heap sort heap An Algorithm is such a data structure designed, approximately one complete binary tree, while having a characteristic value of the parent node is greater than the value of (less than) the child node.

Heap two, the parent node is larger than the child node is called the maximum heap, the parent node is smaller than the minimum child nodes called heap

Here is one of the largest heap

 

 2. heapsort step

The maximum heap example, assume that there are n elements,

1) The maximum stack structure

2) the value of the root node of the n-th switching nodes

3) adjusted to the current maximum heap heap

4) n minus one, continued 2) 3) step, until n == 1

3. How to construct the largest heap

  From the nature of the maximum heap, the value of each of the parent than the child node of a large value, so from the bottom up adjustment after adjustment root is the greatest. for example

Assuming that the array array:   

 

 1) first to imagine the following form (binary tree), ordered in an array or in sequence,

 

 2) construction began maximum heap

  1. The first find the last parent node, since the maximum stack form the above form, it is easy to obtain a final index of the parent node, the above elements are 8

  Then the index of the last parent node: i = 8 / 2-1 = 3, and the subscript of the left child node of the node is: 2i + 1, the right child node subscripts: 2i + 2 This is an important property .

  Comparing corresponding values ​​of i and 2i + 1,2i + 2, and if the two child nodes is greater than a parent node, then the exchange value of the parent and child nodes, and pay attention to judge 2i + 1 and 2i + 2 exists or not (i.e., subscript not out of bounds)

 

   After the first adjustment:

          

 

   the second time:

  

 

 

 

  the third time

  

 

 

 

  the fourth time

  

 

 

 

  In this case, pay attention to the back of the switching nodes may result does not satisfy the maximum heap nature, then continue to the steps above, it is actually a recursive, wait for the next look like to understand the code

  Fifth, the adjustment is completed

  

 

 

  Begin to exchange

3) exchange of the value of the root node ni (i is the number of elements has been exchanged) elements

 

 

 

4) it is then adjusted to a maximum stack, this time from the top down adjustment, adjustment is started from the root node.

 

 

 5) Continue 3), 4) step, until ni == 1, this time the sort is complete

 

Combined with the code over again it is easy to understand the

code show as below:

. 1 #include <stdio.h>
 2  // swap values of the numbers 
. 3  void the swap ( int * A, int * B)
 . 4  {
 . 5      int TEMP * = A;
 . 6      * = A * B;
 . 7      * B = TEMP ;
 8  }
 9  // configured maximum heap process 
10  void maxHead ( int * ARR, int Start, int End)
 . 11  {
 12 is      int the parentNode = Start; // parent node 
13 is      intthe childNode = the parentNode * 2 + . 1 ; // left child node 
14      the while (the childNode <= End)
 15      {    
 16          // the childNode +. 1 is a right child node 
. 17          IF (the childNode + . 1 <= End && ARR [the childNode + . 1 ]> ARR [the childNode] )
 18 is              the childNode ++; // big right child, right child take
 19          // parent node is greater than a child node, without exchange, direct return 
20 is          IF (ARR [the parentNode]> ARR [the childNode])
 21 is              return ;
 22 is          // otherwise , exchange value of the parent node and the child node 
23 is          the else 
24         {    
 25              the swap (& ARR [the parentNode], & ARR [the childNode]);
 26 is              
27              // after switching back may cause node
 28              // not meet the requirements of the maximum heap, so go back to the configuration of the node 
29              the parentNode = the childNode ;
 30              the childNode the parentNode * = 2 + . 1 ;
 31 is          } 
 32      } 
 33 is          
34 is  }
 35  void headSort ( int * ARR, int num)
 36  {    
 37 [      // number of array elements num 
38 is      int I;
 39     // start configured to stack a maximum 
40      for (NUM = I / 2 - . 1 ; I> = 0 ; i-- )
 41 is          maxHead (ARR, I, num- . 1 );
 42 is       
43 is      for (num-I = . 1 ; I> 0 ; i-- )
 44 is      {
 45          the swap (& ARR [I], & ARR [ 0 ]); // exchange the first and the second element i (i from the rear first) element 
46 is          maxHead (ARR, 0 , I- . 1 ); // after the exchange is configured to continue operation of the maximum stack 
47      } 
 48  }
 49  int main ()
50 {
51     int i; 
52     int arr[8]={1,5,0,6,3,9,8,7};
53     headSort(arr,8);//堆排序 
54     for(i=0;i<8;i++)
55         printf("%d\n",arr[i]);
56     return 0;
57 } 

HEAPSORT average time complexity is: O (nlogn)

There are questions, feel free to contact

 

Guess you like

Origin www.cnblogs.com/duichoumian/p/12613153.html