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;} Down adjust function void Siftdown (int i) {//Pass in a node number I that needs to be adjusted downward, here pass in 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 executes while (i*2<=n&&flag==0) {//first to determine its relationship with the left son, and T record value is smaller than the node number if (H[i]>h[i*2]) {T =i*2;} else {t=i;} If it has a right son, then talk to the right son if (i*2+1<=n) {//If the right son's value is small, update the smaller node number if (H[t]>h[i*2+1]) {t=i*2+1;}} If you find that the minimum node number is not your own, the child nodes have a smaller value than the parent node if (t!=i) {swap (t,i);//exchange them, notice that the SWAP function i=t;//Update I is the number of the son node that I just exchanged with it, so that it is easy to continue downward adjustment}else{ flag=1;//Otherwise, the current parent node is already smaller than the two nodes, no further adjustments are required}}//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];//records the value of the heap vertex with a temporary variable h[1]=h[n];//the last point of the heap is assigned to the element of the heap top n--;//heap, reducing 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, continuously delete 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;}// Down adjust function void Siftdown (int i) {//Pass in a node number I that needs to be adjusted downward, here pass in 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 executes while (i*2<=n&&flag==0) {//first to determine its relationship with the left son, and T record value is smaller than the node number if (H[i]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Build the heap, and sort the heap