Heap sort and build maximum heap

Source: Internet
Author: User

The heap is the structure of a fully binary tree, so for a heap with n nodes, the height is O (logn).

Maximum Heap: the largest element in the heap is stored at the root Node's location.

In addition to the root node, the value of each other node is as large as the value of its parent Node. That is, the value of all nodes contained in any subtree is not much more than the value of the tree root Node.

The location number of the nodes in the heap is determined, the root node number is 1, and each layer is numbered from left to Right. By the heap is a complete binary tree, you can know that when a node in the heap is numbered i, if the node has left and right subtree, then the node number of Zuozi is 2*i, and the node number is 2*i+1 (of course, This is the case of the root node number 1).

and the number of leaf nodes in the heap with N nodes is from N/2+1~n. Because assuming that the node n/2+1 is not a leaf node, its left child node number (n/2+1) is *2=n+1, and the node has only n total. The leaf nodes of the complete binary tree only show up at the bottom two Layers. The lowest leaves are concentrated on the left, and the leaves on the bottom two are centered on the Right.

Maintain maximum heap function Max_heapweihu (a,i), Assuming that the left and right subtree of node I is already the largest heap. then, when maintaining the heap, compare the value of the I node to the size of the left and right node values, and swap the maximum values in three numbers to the location of the root Node. Assuming that the root node I and the value of the left Dial hand node exchange, then Zuozi will call Max_heapweihu (a,2*i) again, determine whether Zuozi is the largest heap, if it is the end, otherwise continue to call for Maintenance. therefore, the time complexity of calling Max_heapweihu (a,i) is o (logn).

Heapfy (inta[],intIintHeapsize) {     intlargest=i; intleft=2*i-1; intright=2*i; if(leftA[left]) Largest=left ; if(rightA[right]) Largest=right ; if(largest!=I) {swap (a[i],a[largest]);     Heapfy (a,largest); }}

Build Maximum heap: Converts the a[1,n] array to the maximum heap. Because the maximum heap is a complete binary tree structure, a[n/2+1],......, a[n] is the largest heap of leaf nodes. Each leaf node is itself a maximum heap, so we need to gradually maintain the maximum heap (call max_heapweihu (a,i) maintenance) from a[n/2]~a[1].

void buildheap (int a[],int  len) {      for (int i=len/2 -1; i>=0; i--)          heapfy (a,i,len);}        

Heap Sort: First build a maximum heap. Then swap the maximum heap of a[1] with a[n] and remove the node n from the heap, by reducing the value of a.heap_size. In the remaining nodes, the new root node may violate the nature of the maximum heap, so it is necessary to call Max_heapweihu (a,1) to maintain the maximum heap.

void sortheap (int a[],int  len) {       for (int i=n-1; I >=0; i--)    {         swap (a[0],a[i]);         Heapfy (a,0, i);    }}    
#include <iostream>using namespacestd;voidHeapfy (inta[],intIndexintHeapsize) {    intleft=index*2+1; intright=left+1; intlargest=index; if(leftA[left]) Largest=left ; if(rightA[right]) Largest=right ; if(largest!=Index)        {swap (a[index],a[largest]);    Heapfy (a,largest,heapsize); }}voidHeapsort (inta[],intLen) {     for(inti=len/2-1; i>=0; i--) {heapfy (a,i,len); }     for(inti=len-1; i>=0; i--) {swap (a[i],a[0]); Heapfy (a,0, i); }}intmain () {inta[Ten]={9,3,4,5,1,8,0,2,7,6}; Heapsort (a,Ten); }

Heap sort and build maximum heap

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.