C + + implementation Heap class and heap sorting

Source: Internet
Author: User
Tags int size

Take a simple sort summary: SELECT, insert, swap sort, publish your own heap class today, and heap sort on an array using the heap,

Unlike a simple sort method, the time complexity of a heap sort is nlog2n, while a simple sort requires n2.

/*heap.h*//* Uses a pointer to an array to implement a heap/* heap: is a completely binary tree, in addition to the lowest layer, each layer is slow, the lowest layer of nodes left on the * to meet the heap order properties, each parent node is greater than or equal to children's data items * Note: In the heap implementation of the internal array, I start to store data from array[1] * * Delete the maximum Item method: Swap the first and last data of an array, and the length of the array is reduced by 1, at which point the heap is no longer a standard heap and needs to be lowered * Add a data item: Add the data to the end of the array, and it may no longer be a standard heap, Need to be raised/#ifndef heap_sort_h #define HEAP_SORT_H 1 #include <iostream> #include <cstdlib> Template<typena Me t> class Heap {public://default constructor Heap (): M_array (0), m_size (0), M_lenght (0) {}//Allocating length array Heap (int length ); destructor ~heap () {delete [] M_array}///Insert a data item bool Insert (T item); Determine whether an empty bool empty (); Get the maximum data, but do not delete T Fetchmax (); Delete the largest data is the first data item of the array void Deletemax (); Displays the contents of the heap void display (); Converts an array of type T into a heap void Array_heapsort (t* array, int size); Heap sort//precondition: The heap already exists void heapsort (); Private://downward, the [star,end] interval of the two-fork tree heap void locate_down (int start,int end); The two-fork tree in the [Star,end] interval is raised to heap void locate_up (int start,int end); private:t* M_array; pointer int m_size pointing to an array; The number of the data has been stored int m_lenght; The length of the allocated space};#include "Heap.cpp" #endif

/*heap.cpp*/template<typename t> heap<t>::heap (int length) {m_array=new t[length+1]; m_size=0 Length } template<typename t> T Heap<t>::fetchmax () {return m_array[1];} template<typename T> bool Heap&lt ; T>::empty () {return m_size==0.} template<typename t> void heap<t>::locate_down (int start,int end) {int c=start*2; int temp; while (C<=end) {if (c<end && m_array[C]<m_array[c+1]) c=c+1; if (m_array[start]<m_array[c] {temp=m_array[start]; m_array[start]=m_array[c]; m_array[c]=temp; start=c; C=2*c; } template<typename t> void heap<t>::d Eletemax () {m_array[1]=m_array[m_size];//tail cover first--m_size;//length Minus one lo Cate_down (1,m_size); Downgrade, guaranteed heap structure} template<typename t> void heap<t>::locate_up (int start, int end) {int loc=end; int PARENT=END/2 ; int temp; while (Parent>=start && m_array[Loc] >m_array[parent]) {temp=m_array[loc]; m_array[Loc]=m_array[parent]; m_array[parent]=temp; Loc=parent; PARENT=LOC/2; } template<typename t> bool Heap<t>::insert (T Item) {if (m_size>m_lenght)//To determine if full {std::cerr<< " The heap has full/n "; This->~heap (); return 0; } ++m_size; Starting with data from m_array[1], ++m_size m_array[m_size]=item first; LOCATE_UP (1,m_size); Raise the guaranteed heap structure return 1; Template<typename t> void Heap<t>::d isplay () {int i=1; while (i<=m_size) {std::cout<<m_array[I ]<< ""; ++i; } std::cout<<std::endl; } template<typename t> void Heap<t>::array_heapsort (t* array, int size)//array[] Start storing data {int i; m from array[0] _lenght=size; M_array=new T [m_lenght+1]; for (I=1;i<=size;++i) This->insert (array[i-1]); This->heapsort (); Write to the original array for (i=1;i<=m_size;++i) array[i-1]=m_array[i]; } template<typename t> void Heap<t>::heapsort () {int i,temp; for (i=m_size;i>=2;--i) {temp=m_array[1] ; m_array[1]=m_array[i]; m_array[i]=temp; Locate_down (1,i-1); Locate_down () will 1~i-1 the data inside the stack}

/*test.cpp*/#include "heap.h" #include <iostream> using Std::cout; Using Std::cin; Using Std::endl; int main () {int i; Heap<int> One (6); for (I=0;i<6;++i) One.insert (i+1); cout<< "Built heap is:/n"; One.display (); cout<< "After heap sort for:/n"; One.heapsort (); One.display (); cout<< "The following is a heap sort on an array:/n"; int a[]={7,9,8,6,10,11}; cout<< "The array to be sorted is:/n"; for (i=0;i<6;++i) cout<<a[i]<< ""; cout<<endl; cout<< "After heap sort for:/n"; Heap<int> two; Two.array_heapsort (a,6); for (i=0;i<6;++i) cout<<a[i]<< ""; cout<<endl; return 0; }

The results are:

The built heap is: 6 4 5 1 3 2 after the heap sort for: 1 2 3 4 5 The following is a heap sort on an array: the array to be sorted is: 6 7 9 8 6, 10 to heap sort after: 11 6 7 8 9 10

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.