Heap sorting and Analysis

Source: Internet
Author: User
Preface

Remember to use code to implement algorithms when learning data structures. The important thing is that the written code has a correct input and a correct output, then it is quite satisfying. I read a lot of code from the Internet. After reading the code, I seem to understand it. After I finish writing it, I am correct, but soon I forget it because when my brain is recalling it, just vaguely remember the part of the code, so fuzzy, you can't write the correct code again, maybe the first time you write it, It's because someone else's code is referenced, after reading it, the brain can have a short high-definition memory, so it cheated me and thought that what I wrote satisfied my sense of accomplishment. But the code is recognized by computers, and we prefer text and images. Therefore, when learning algorithms, we need to focus on algorithm principles and algorithm analysis, express them in text and images, and then convert the text into code when necessary. Memory is divided into three steps: encoding, storage and retrieval. Taking learning as an example, we first understand the knowledge, then summarize the knowledge, and finally consolidate the knowledge to facilitate the retrieval of knowledge for future applications.

Heap sorting process

The heap is divided into a large heap and a small heap, which isFull Binary Tree. The requirement of a large root heap is that the value of each node is not greater than that of its parent node, that isA [PARENT [I]> = A [I].In the non-descending sorting of arrays, you need to use the big root heap, because according to the requirements of the big root heap, the maximum value must be at the top of the heap.

Since it is heap sorting, it is natural to create a heap first, and the core content of heap building is to adjust the heap, make the binary tree meet the definition of the heap (the value of each node is not greater than the value of its parent node ). The heap adjustment process should start fromLast non-leaf nodeFirst, assume there are arrays A = {1, 3, 4, 5, 7, 2, 6, 8, 0 }. For example, the array subscript starts from 0 and starts from A [3] = 5. Compare the size with the left and right children respectively. If A [3] is the largest, you do not need to adjust it. Otherwise, it is the highest exchange position with the value of the child, in Figure 1, there is A [7]> A [3]> A [8], so A [3] and A [7] are switched from Figure 1.1 to Figure 1.2.

So the heap building process is

   1: for ( i = headLen/2; i >= 0; i++)

   2:  

   3:        do AdjustHeap(A, heapLen, i)

Heap adjustment: if the initial array is not sorted in descending order, you do not need to tune the heap. This is the best case. The running time is executor (1 ); if the initial array is 1.5, only A [0] = 1 does not meet the heap definition. After comparison with the subnode, it is adjusted to Figure 1.6, but Figure 1.6 still does not meet the heap definition, therefore, recursive adjustment is required until the definition of the heap is satisfied or until the heap bottom is reached. It is the worst case if the heap is recursively tuned to the bottom of the heap. The running time is O (h) (h is the height of the node to be adjusted, and the heap bottom height is 0, the heap height is floor (logn )).

After the heap is created, heap 1.7 is a large heap. Exchange A [0] = 8 with A [heapLen-1], then heapLen minus one, 2.1, then AdjustHeap (A, heapLen-1, 0), 2.2. So swap the first dollar of the heap

The last element of the heap, and then the heapLen of the heapLen is reduced by one. The heapLen heap size is adjusted to heapLen until heapLen = 1, finally, result 3 is obtained.

   1: /*

2: input: array A, heap length hLen, and node I to be adjusted

3: function: heap Adjustment

   4: */

   5:  

   6: void AdjustHeap(int A[], int hLen, int i)

   7: {

8: int left = LeftChild (I); // left child of node I

9: int right = RightChild (I); // The right child node of node I

  10:     int largest = i;

  11:     int temp;

  12:  

  13:     while(left < hLen || right < hLen)

  14:     {

  15:         if (left < hLen && A[largest] < A[left])

  16:         {

  17:             largest = left;

  18:         }

  19:         

  20:         if (right < hLen && A[largest] < A[right])

  21:         {

  22:             largest = right;

  23:         }

  24:  

25: if (I! = Largest) // if the maximum value is not the parent node

  26:         {

27: temp = A [largest]; // exchange the parent node and the child node with the maximum value

  28:              A[largest] = A[i];

  29:              A[i] = temp;

  30:  

31: I = largest; // a new parent node for iterative heap

32: left = LeftChild (I); // new subnode

  33:             right = RightChild(i);

  34:         }

  35:         else

  36:         {

  37:             break;

  38:         }

  39:     }

  40: }

  41:  

  42: /*

43: input: array A, heap size hLen

44: function: heap Creation

  45: */

  46: void BuildHeap(int A[], int hLen)

  47: {

  48:     int i;

49: int begin = hLen/2-1; // the last non-leaf node.

  50:     for (i = begin; i >= 0; i--)

  51:     {

  52:         AdjustHeap(A, hLen, i);  

  53:     }

  54: }

  55:  

  56: /*

57: input: array A, the size of the array to be sorted aLen

58: function: heap sorting

  59: */

  60: void HeapSort(int A[], int aLen)

  61: {

  62:     int hLen = aLen;

  63:     int temp;

  64:  

65: BuildHeap (A, hLen); // create A heap

  66:  

  67:     while (hLen > 1)

  68:     {

69: temp = A [hLen-1]; // swap the first element of the heap and the last element of the heap

  70:         A[hLen-1] = A[0];

  71:         A[0] = temp;

72: hLen --; // The heap size minus one.

73: AdjustHeap (A, hLen, 0); // tune the heap

  74:     }

  75: }

Performance Analysis

  • Heap adjustment:As analyzed above, the heap Runtime is O (h ).
  • Heap Creation: The maximum number of nodes in each layer is n1 = ceil (n/(2 ^ (h + 1 ))),

Therefore, the heap creation time is O (n ).

  • Loop heap (Code 67-74)Because the top element of the heap needs to be tuned, the running time is O (h) = O (floor (logn )). Therefore, the running time of the cyclic heap is O (nlogn ).

Total running time T (n) = O (nlogn) + O (n) = O (nlogn ). For the best and worst cases of heap sorting, because the worst and best input only affects the heap creation time O (1) or O (n ), the process of cyclic heap adjustment is an important proportion in the overall time, that is, O (nlogn) + O (1) = O (nlogn) + O (n) = O (nlogn ). Therefore, in the best or worst case, the running time of heap sorting is O (nlogn ). In addition, the heap sorting algorithm is still in-place algorithm ).

End

If you do not know much about O, NLP, and other progressive symbols, you can refer to blog: Progressive notation of computer algorithm analysis. The above Article is my study notes. If you have any mistakes, please kindly advise me.

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.