Deep understanding of heap sorting and Analysis

Source: Internet
Author: User

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

Copy codeThe Code is as follows: for (I = headLen/2; I> = 0; I ++)

Do AdjustHeap (A, heapLen, I)

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.

Copy codeThe Code is as follows :/*
Input: array A, heap length hLen, and node I to be adjusted
Function: heap Adjustment
*/

Void AdjustHeap (int A [], int hLen, int I)
{
Int left = LeftChild (I); // left child of node I
Int right = RightChild (I); // The right child node of node I
Int largest = I;
Int temp;

While (left {
If (left {
Largest = left;
}

If (right {
Largest = right;
}

If (I! = Largest) // if the maximum value is not the parent node
{
Temp = A [largest]; // exchange the parent node and the child node with the maximum value
A [largest] = A [I];
A [I] = temp;

I = largest; // a new parent node for iterative heap
Left = LeftChild (I); // new subnode
Right = RightChild (I );
}
Else
{
Break;
}
}
}

/*
Input: array A, heap size hLen
Function: heap Creation
*/
Void BuildHeap (int A [], int hLen)
{
Int I;
Int begin = hLen/2-1; // the last non-leaf node.
For (I = begin; I> = 0; I --)
{
AdjustHeap (A, hLen, I );
}
}

/*
Input: array A, the size of the array to be sorted aLen
Function: heap sorting
*/
Void HeapSort (int A [], int aLen)
{
Int hLen = aLen;
Int temp;

BuildHeap (A, hLen); // heap Creation

While (hLen> 1)
{
Temp = A [hLen-1]; // swap the first element of the heap and the last element of the heap
A [hLen-1] = A [0];
A [0] = temp;
HLen --; // reduce the heap size by one
AdjustHeap (A, hLen, 0); // tune the heap
}
}

Performance Analysis
• Heap tuning: As analyzed above, the heap running time 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 ).
• Cyclic heap calling (Code 67-74). Because the heap top element 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 ).

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.