Build the heap, and sort the heap

Source: Internet
Author: User

Build heaps, and heap sortCode Listing 1:
#include <stdio.h>int h[101];//the array int n;//used to hold the heap to store the number of elements in the heap, that is, the size of the heap//Exchange function, to swap the value of two elements in the heap void swap (int x,int y) { int t;t=h[x];h[x]=h[y];h[y]=t;} Downward adjustment function void Siftdown (int i) {//Pass in a node number I that needs to be adjusted downward, here pass 1. That is, starting from the vertex of the heap, adjust the int t,flag=0;//flag to mark whether it is necessary to continue the downward adjustment//when the I node has a son (in fact, there is at least a left son) and there is a need to continue the adjustment when the loop runs while (i*2<=n&& flag==0) {//First infer its relationship with the left son. and the T record value is smaller than the node number if (H[i]>h[i*2]) {t=i*2;} else {t=i;} Suppose it had a right son, and then a discussion on the right son if (i*2+1<=n) {//Suppose the value of the right son is small. Update the smaller node number if (H[t]>h[i*2+1]) {t=i*2+1;}} Suppose you find that the smallest node number is not yourself, stating that there are smaller values in the child nodes than the parent node if (t!=i) {swap (t,i);//exchange them. Note that the SWAP function i=t;//Update I is the number of the son node that was exchanged with it just now. Make it easy to continue down the adjustment}else{flag=1;//otherwise the current parent node is smaller than the two nodes, no need to adjust}}}//Build heap function void creat () {int i;for (i=n/2;i>=1;i--) {// The total last non-leaf node to the first node is adjusted upward siftdown (i);}} Delete the largest element int Deletemax () {int t;t=h[1];//record the value of the heap vertex with a transient variable h[1]=h[n];//the last point of the heap to the element of the heap top n--;//heap, decrease by 1 siftdown (1);//Downward adjustment Return t;//returns the maximum value of the vertex of the previously recorded heap}int main () {int i,num;scanf ("%d", &num), for (i=1;i<=num;i++) {scanf ("%d", &h[i]) ;} N=num;creat (); for (i=1;i<=num;i++) {Delete the top element and delete it continuously n times, in fact, from the large to the small number of output printf ("%d", Deletemax ());} return 0;}
Code, 2:
#include <stdio.h>int h[101]; The array used to store the heap int n;//is used to store the number of elements in the heap, that is, the heap size//interchange function, which is used to swap the values of two elements in the heap void swap (int x,int y) {int t;t=h[x];h[x]=h[y];h[y]=t;}// Downward adjustment function void Siftdown (int i) {//Pass in a node number I that needs to be adjusted downward, here 1, that is, starting from the vertex of the heap, adjust the int t,flag=0;//flag to mark whether it is necessary to continue downward adjustment//When I node has son ( In fact, there is at least one left son) and there is a need to continue to adjust when the loop runs while (i*2<=n&&flag==0) {//First infers its relationship with the left son, and the T record value is smaller than the node number if (H[i]


Build the heap, and sort the 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.