Heap definition: If the array a[1,.... n] satisfies:a[i]>a[2*i] && a[i]>a[2*i+1],1<=i<=n/2, then a heap , and it's max-heap.
There are two types of heap,max-heap and Min-heap, of which min-heap are of the opposite nature as described above, i.e. A[i]<a[2*i] && a[i]<a[2*i+1].
Here, Max-heap is an example of the three basic operations of the HEAP, namely max-heap-maintenance, Build-max-heap, Heapsort.
1. max-heap-maintenance
Input: Array A and subscript indice (i) Output: maintains the max-heap nature of the subtree with the root of I.
The pseudo-code for Max-heap-maintenance is as follows:
Use an example diagram to explain the operation of the above program:
a) figure is the initial state of the HEAP, and now executes program max-heap-maintenance (a,2). The value of Node 2 is 4, which is smaller than the left child's value of 14, so the a[2] and a[4] are exchanged, although the right child value is 7, also greater than a[2], but we are looking for two children with the most value of the child, and the parent node and exchange it. After the exchange is completed, from the location of the child node being exchanged, the figure is a[4], see Figure B), and then repeat the above operation on the node until the node is a leaf node, C), then terminate the program.
2. Build-max-heap
Because each time the max-heap-maintenance (i) operation is performed, it is actually a subtrees tree that establishes the maximum Heap with I as the root node. So we build the maximum heap from the bottom up, only in this way can we guarantee the max-heap nature of the heap built.
3. Heap-sort
First build the max-heap, but we still can not get the sort result according to Max-heap, because Max-heap can only guarantee the root node value is greater than the child node value, and there is no child node size relationship between the comparison. See Pseudo code + Sample analysis diagram.
Finally, paste the C + + implementation code:
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int a[101]; 5 int array_size,heap_size; 6 int Left (int i) {7 return 2*i, 8} 9 int right (int i) {ten return 2*i+1;11}, max_heap_maintenance (int i) {1 3 int L, R, largest=-1;14 l=left (i), r=right (i), and if (l>heap_size| | R>heap_size) return;17 if (L<=heap_size&&a[l]>a[i]) {largest=l;19}20 else largest=i if (R<=heap_size&&a[r]>a[largest]) {largest=r;23}24 if (largest!=i) {+ Int. tmp=a[largest];26 a[largest]=a[i];27 a[i]=tmp;28 max_heap_maintenance (largest); 29} 30}31 void Build_max_heap () {heap_size=array_size;33 for (int i=array_size/2;i>=1;i--) {Max_heap_maintenan CE (i);}36}37 void Heap_sort () {build_max_heap (); (int i=array_size;i>=2;i--) {+-int tmp= a[i];41 a[i]=a[1];42 a[1]=tmp;43 HeAp_size--;44 max_heap_maintenance (1);}46 int tmp=a[2];47 a[2]=a[1];48 a[1]=tmp;49}50 int main ( {scanf ("%d", &array_size) {!=eof ("%d") {i=1;i<=array_size;i++) {scanf Mp;a[i]);}55 heap_sort (); +/-(int i=1;i<=array_size;i++) {$ printf ("%d", A[i]) ;}printf ("\ n");}60 return 0;61}Code-heapsort
Introduction to Algorithmic Learning-heapsort