Deep understanding of heap sequencing and its analysis _c language

Source: Internet
Author: User

Remember in the study of data structure when blindly want to use code to achieve the algorithm, the importance of the written code has a correct input, and then have a correct output, then very satisfied. Read a lot of code from the Internet, after looking at the seemingly understand, I wrote it after the correct, but soon forgot, because the brain in memory, only vaguely remember the part of the code, so vague, can not write the correct code again, perhaps the first time it was written because of reference to someone else's code, After seeing the brain can carry on a short period of high clear memory, so deceived me, thinking that they write out, satisfied with the sense of achievement. But the code is computer-recognized, and we prefer text, images. So we should pay attention to the algorithm in the study of the principle and algorithm analysis, with text, image expression, and then when needed to use the text into the code. Memory is divided into three steps: coding, storage and retrieval, take learning as an example, first understand knowledge, then inductive knowledge, finally consolidate knowledge, for future applications and facilitate the retrieval of knowledge.

Heap Sort Process
The heap is divided into Dagen and small Gan, which are completely binary trees . The requirement for Dagen is that the value of each node is not the value of its parent node, i.e. A[parent[i]] >= a[i]. in a non descending sort of array, you need to use a large root heap, because according to Dagen's requirements, the maximum value must be on the top of the heap.

Since it is a heap sort, nature needs to build a heap first, and the core of the heap is to adjust the heap so that the two-fork tree satisfies the definition of the heap (the value of each node is less than the value of its parent node). The stacking process should start with the last non-leaf node , assuming that there are array a = {1, 3, 4, 5, 7, 2, 6, 8, 0}. Then the stacking process is as follows, and the array subscript starts at 0, a[3] = 5. Compare size with left and right children, if a[3] is maximum, then there is no adjustment, otherwise the largest exchange position with the value of the child, in Figure 1 is a[7] > A[3] > A[8], so a[3] swap with a[7, from Figure 1.1 to figure 1.2.

So the process of building a heap is

Copy Code code as follows:

for (i = HEADLEN/2 i >= 0; i++)

Do Adjustheap (A, Heaplen, i)


After the heap is finished, the heap, like Figure 1.7, is a large heap. Swap a[0] = 8 with a[heaplen-1], then Heaplen minus one, as shown in Figure 2.1, then Adjustheap (A, heapLen-1, 0), as shown in Figure 2.2. So swap the first dollar of the heap
Elements and the last element of the heap, then the heap size Heaplen minus one, the heap of the size of the Heaplen heap to be stacked, so that the cycle until Heaplen = = 1 o'clock stopped, and finally came to the result as shown in Figure 3.

Copy Code code as follows:

/*
Input: Array A, the length of the heap Hlen, and the node I need to adjust
function: Stack
*/

void adjustheap (int a[], int hlen, int i)
{
int left = Leftchild (i); Left child of Node I
int right = Rightchild (i); Node i's right child node
int largest = i;
int temp;

while (Left < Hlen | | Right < Hlen)
{
if (left < Hlen && A[largest] < A[left])
{
largest = left;
}

if (Right < Hlen && A[largest] < A[right])
{
largest = right;
}

if (I!= largest)//if the maximum value is not a parent node
{
temp = A[largest]; Swap the parent node and the child node with the maximum value
A[largest] = A[i];
A[i] = temp;

i = largest; New parent node for iterative heap generation
left = Leftchild (i); New child node
right = Rightchild (i);
}
Else
{
Break
}
}
}

/*
Input: Array A, heap size Hlen
Function: Build a heap
*/
void buildheap (int a[], int hlen)
{
int i;
int begin = HLEN/2-1; 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
Features: Heap Sort
*/
void heapsort (int a[], int aLen)
{
int hlen = ALen;
int temp;

Buildheap (A, Hlen); Build a heap

while (Hlen > 1)
{
temp = a[hlen-1]; The first element of the swap heap and the last element of the heap
A[hlen-1] = a[0];
A[0] = temp;
hlen--; The size of the heap minus one
Adjustheap (A, Hlen, 0); Stack
}
}

Performance analysis
• Stacking: The above has been analyzed, the running time of the stack is O (h).
• Build heap: The maximum number of nodes per layer is N1 = Ceil (n/(2^ (h+1))),

Therefore, the runtime of the build Heap is O (n).
• Cyclic heap (code 67-74) because the heap top element needs to be tuned, so the runtime is O (h) = O (Floor (LOGN)). So the running time of the cyclic stack is O (nlogn).
Total elapsed time t (n) = O (nlogn) + O (n) = O (Nlogn). For the best of heap sorting and worst-case runtime, because the worst and best input will only affect the build heap's Run time O (1) or O (n), and in the overall time is an important proportion of the process of cyclic stacking, that is, O (nlogn) + O (1) =o (NLOGN) + O (n) = O ( NLOGN). So the best or worst case scenario is that the runtime of the heap sort is O (Nlogn). And the heap sort or the in-situ algorithm (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.