previous blogs introduced two sorting algorithms for arrays: Insert sort and merge sort (with recursion), see link http://blog.csdn.net/u013165521/article/details/46845033.
In this blog post, there is also a sort algorithm: heap ordering.
(Introduction to Content-based algorithms)
first, the concept of the heap
The so-called Heap, which is an array, can also be seen as an approximate fully binary tree. An element of the corresponding array for each node in the tree. The binary heap is divided into two types: maximum heap and minimum heap. This article mainly describes the maximum heap, similar to the minimum heap. Maximum heap characteristics : For a random node, the value of the node is greater than the left child, right child value, but the value of the child is not required.
second, heap sorting algorithmThe heap sorting algorithm calls the function build_max_heap the input array ARRAY[1..N] to build piles. where n indicates the length of the array. Since the heap is established, the largest element of the array is stored at the root node a[1], which is exchanged by a[1] with the last element of the array. Moves the largest element back, implementing sorting.
However, after swapping the new root node may not meet the characteristics of the heap, it is necessary to call the child function max_heapify to the remaining array elements of the maximum heap nature of maintenance. heap sorting algorithm. By constantly repeating this process (n-1), the array is sorted from small to large (due to the maximum heap used).
For a brief introduction to the two sub-functions mentioned above.
function the role of Build_max_heap: build a heap .
Because sub-array a (N/2+1..N) is a leaf node of a tree, there is no need for heap maintenance.
So. Only the A[1..N/2] array elements need to be maintained. You can build the heap. function max_heapify: the maintenance heap. Procedure: If a[i] represents a node of the tree, then A[2*i] is its left child,A[2*i+1] is its right child. Next, the size of the three selected the largest element of the subscript, deposited in the largest. And then. Inference (largest==i). If not satisfied, the element is exchanged. Moves a large element up.
At this point, a subtree with a[largest] as the root node may not satisfy the nature of the heap, so it is necessary to recursively call itself.
Third, the implementation of the algorithm The above describes the heap implementation of array sorting principle, the following direct source code.
#include <stdlib.h> #include <stdio.h> #include <iostream>using namespace std;void Swap (int *x, int *y) void max_heapify (int array[], int i, int heap_size), void build_max_heap (int array[],int len), void heapsort (int array[],i NT Len); void Swap (int *x, int *y) {int temp;temp=*x;*x=*y;*y=temp;} void max_heapify (int array[], int i, int heap_size) {int largest;int _left=2*i;int _right=2*i+1;if (_left<=heap_size &A mp;& Array[_left]>array[i]) {largest=_left;} Elselargest=i;if (_right<=heap_size && array[_right]>array[largest]) {largest=_right;} if (largest!=i) {Swap (&array[largest],&array[i]); Max_heapify (array,largest,heap_size);}} void build_max_heap (int array[],int len) {int heap_size=len;for (int i=len/2; i>=1; i--) {max_heapify (array,i,heap_ size);}} void heapsort (int array[],int len) {int heap_size=len; Build_max_heap (Array,len); for (int i=len; i>=2; i--) {Swap (&array[1],&array[i]); heap_size--; Max_heapify (array,1,heap_size);}} void Main () {int aRray[]={0,14,10,8,7,9,3,2,4,1};int len=9; Heapsort (Array,len);cout<< "heap sort result:\n"; for (int i=1; i<=len; i++) {cout<<array[i]<< "";} Cout<<endl;}
Experimental results:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
Heap sequencing (C language Implementation)