Read, think, write code!
/********************************************* * [email protected] * Blog:http://blog.csdn.net/hustyangju * Title: Heap sequencing Implementation, in addition to implement interface: Take heap maximum and delete, heap insert * idea: The heap is implemented on the original site of the sequential array, using the nature of the complete binary tree, the definition of the maximum heap and minimum heap is implemented. * Classic Application scenario: In-memory heap data management * Spatial complexity: Heap sequencing is implemented on the site, 0 * Time complexity: Heap ordering O (n LGN), take the most value O (1), insert the worst O (LGN) *********************************** /#include <iostream> #include <algorithm>using namespace::std;//the definition of the implementation class for the heap sort class heapsort{ Public:heapsort (int *parray, int narraysize);//constructor ~heapsort ();//destructor Private:int *m_pA;//poin TS to a array int m_nheapsize;//stands for the size public:void buildmaxheap (); Build a heap void sort ();//Build a maximum heap and sort, in order (from small to large) placed in the original array int popmaxheap ();//Take maximum heap maximum value void insertmaxheap (int a);//Plug into a new value to the maximum heap, is actually to add a value at the end of the element, and then maintain the nature of the maximum heap void print ();//sequential output array protected:int leftchild (int node);//take Left child subscript int Rightchi LD (int node);//Right child subscript int parent (int node);//Take parent node subscript void maxheapify (int nIndex);//justify the heap};//constructor initializes HEAPSO Rt::heApsort (int *parray, int narraysize) {m_pa = Parray; M_nheapsize = narraysize;} destructor Heapsort::~heapsort () {}//take left child subscript, note lineage array starting from 0 custom int heapsort::leftchild (int node) {return 2*node + 1;//the array St Arts from 0}//take right child subscript int Heapsort::rightchild (int node) {return 2*node + 2;} Take the parent node subscript int heapsort::P arent (int node) {return (node-1)/2;} Use recursion to maintain the nature of the maximum heap, provided that the maximum heap has been built, and that the function void heapsort::maxheapify (int nIndex) {int nleft = Leftchild (NIndex) is called only on the node of the change; int nright = Rightchild (NIndex); int nlargest = NIndex; if ((Nleft < M_nheapsize) && (M_pa[nleft] > M_pa[nindex])) nlargest = Nleft; if ((Nright < M_nheapsize) && (M_pa[nright] > M_pa[nlargest])) nlargest = Nright; if (nlargest! = NIndex)//If there is a node change to continue the recursive {swap<int> (M_pa[nindex], m_pa[nlargest]); Maxheapify (nlargest); }//constructs the largest heap, the idea: for a complete binary tree, the Subarray a[int ((n-1)/2) +1]~a[n-1] is the leaf node//a[0]~a[int ((n-1)/2)] is a non-leaf node, from bottom to top, starting from the last non-leaf node to maintain the properties of the largest heap void Heapsort:: Buildmaxheap () {if (M_pa = = NULL) return; for (int i = (m_nheapsize-1)/2; I >= 0; i--) {maxheapify (i); }}//constantly take maximum heap Max A[0] with the last element interchange, place the maximum value behind the array, order the array void Heapsort::sort () {if (M_pa = = NULL) return; if (m_nheapsize = = 0) return; for (int i = m_nheapsize-1; i > 0; i--) {swap<int> (M_pa[i], m_pa[0]); M_nheapsize-= 1;//This expression is destructive!!! Maxheapify (0); }}//Remove the maximum value and delete int heapsort in the heap::P opmaxheap () {/*if (M_pa = = NULL) return; if (m_nheapsize = = 0) return; */int a= m_pa[0]; M_PA[0]=M_PA[M_NHEAPSIZE-1]; M_nheapsize-= 1; Maxheapify (0); return A; }//Insert a value, thinking: Put in the last surface of the array (conforming to the common sense of the array), and then back to the layer to maintain the nature of the maximum heap void heapsort::insertmaxheap (int a) {/* if (M_PA = = NULL) retur N if (m_nheapsize = = 0) return; */M_nheapsize + = 1; M_pa[m_nheapsize-1]=a; int index=m_nheapsize-1; while (index>0) {if (m_pa[inDex]>m_pa[parent (Index)]) {swap (M_pa[index], m_pa[parent (index)]); Index=parent (index); } else index=0;//Note here that a layer already satisfies the nature of the maximum heap, it does not need to backtrack}}}//sequential output array void Heapsort::p rint () {for (int i=0;i <m_nheapsize;i++) cout<<m_pa[i]<< ""; cout<<endl; } int main () {int a[10]={6,5,9,8,1,0,3,2,7,4}; int Max; cout<< "Input an array::" <<endl; for (int i=0;i<10;i++) cout<<a[i]<< ""; cout<<endl; Heapsort myheap (a,10); Myheap.buildmaxheap (); cout<< "Pop the max Number:" <<endl; cout<< "The max=" <<myheap.popmaxheap () <<endl; cout<< "after pop:" <<endl; Myheap.print (); Myheap.insertmaxheap (11); cout<< "Insert a number and sort:" <<endl; Myheap.sort (); Myheap.print (); for (int i=0;i<10;i++) cout<<a[i]<< ""; cout<<endl; }
Test results:
GitHub one-day algorithm: Heap algorithm interface Implementation (heap sorting, heap insertion, and heap fetch Max and delete)