GitHub day, one arithmetic problem: Heap algorithm interface (heap sorting, heap insertion and stacker max value, and delete)

Source: Internet
Author: User

Read, think, write code!

/********************************************* * [email protected] * Blog:http://blog.csdn.net/hustyangju * Title: Heap sequencing Implementation, in addition to implement the interface: the maximum heap value and delete, heap insert * idea: The heap is implemented in the sequential array site. Take advantage of the nature of the completely binary tree. The definition of the maximum heap and minimum heap is implemented. * Classic Scenario: In-memory heap data management * Spatial complexity: Heap sequencing is implemented on the site, 0 * Time complexity: The heap is ordered to O (n LGN), and the value O (1) is taken. Insert the worst O (LGN) *********************************************/#include <iostream> #include <algorithm> Using namespace::std;//definition of a heap sort implementation class heapsort{Public:heapsort (int *parray, int narraysize);//constructor ~hea Psort ();//destructor private:int *m_pa;//points to a array int m_nheapsize;//stands for the size public:void B Uildmaxheap (); 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);//Insert a new value into the maximum heap, in fact, add a value at the end of the element, and then maintain the nature of the maximum heap Voi D print ();//sequential output array protected:int leftchild (int node);//take Left child subscript int rightchild (int node);//Take Right child subscript int Parent (int n ODE);//Take parent node subscript void maxheapify (int nIndex);//justify the heap};//constructor initializes heapsort::hEapsort (int *parray, int narraysize) {m_pa = Parray; M_nheapsize = narraysize;} destructor Heapsort::~heapsort () {}//takes the left child subscript. Note Lineage array starting from 0 custom int heapsort::leftchild (int node) {return 2*node + 1;//The array starts from 0}//take right child subscript int Heapsort::right Child (int node) {return 2*node + 2;} Take the parent node subscript int heapsort::P arent (int node) {return (node-1)/2;} The nature of the maximum heap is maintained using recursion. The premise is that the largest heap has been built.     Call the function void heapsort::maxheapify (int nIndex) {int nleft = Leftchild (NIndex) 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)//Assume that there is a node change to continue the recursive {swap<int> (M_pa[nindex], m_pa[nlargest]);     Maxheapify (nlargest); }//build the largest heap, thinking: For a completely 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, start with the last non-leaf node to maintain the properties of the maximum 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.     Ordered 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. Idea: Put on the last side of the array (conforming to the common sense of the array), and then maintain the properties of the maximum heap by layer-by-level heapsort::insertmaxheap (int a) {/* if (M_PA = = NULL) return; 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 (Ind ex)]); Index=parent (index); } else index=0;//note here. 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<&lt ;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:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahvzdhlhbmdqdq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Copyright notice: This article Bo Master original article. Blogs, without consent, may not be reproduced.

GitHub day, one arithmetic problem: Heap algorithm interface (heap sorting, heap insertion and stacker max value, and delete)

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.