PPT (original):
https://files.cnblogs.com/files/eastblue/heap sort. pptx
Video (original):
https://www.bilibili.com/video/av16199074/
Code:
#include <cstdio>#include<cstring>Const intmaxn=1e9+7;intb[50050];voidFunintA[],intI//a array I node completes the operation{ intL=sizeof(a), t; while(2*i+1<l) {T=A[i]; if(2*i+2<L)//There are two child nodes { if(a[2*i+1]<=a[2*i+2]&&a[2*i+1]<a[i])//swap with left child{A[i]=a[2*i+1]; a[2*i+1]=T; I=2*i+1; } Else if(a[2*i+2]<a[2*i+1]&&a[2*i+2]<a[i])//swap with Right child{A[i]=a[2*i+2]; a[2*i+2]=T; I=2*i+2; } Else Break; } Else//only one child node { if(a[2*i+1]<A[i]) {A[i]=a[2*i+1]; a[2*i+1]=T; I=2*i+1; } Else Break; } } }voidHeapsort (inta[]) { intL=sizeof(a); for(intI= (l2)/2; i>=0; i--)//array starting from 0, so ' filter ' from (L-2)/2 (second step for PPT){fun (a,i); } for(intI=1; i<=l;i++)//(third step for PPT){b[i-1]=a[0]; a[0]=a[l-i]; A[l-i]=MAXN; Fun (A,0); } for(intI=0; i<l;i++) A[i]=b[i];} intMain () {intarray[8]={ the, -, $, the, the, -, -, the}; Heapsort (array); for(intI=0;i<8; i++) printf ("%d", Array[i]); }
Heap sequencing of data structures