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.
This blog introduces another sort algorithm: Heap ordering. (Introduction to the Content reference algorithm)
first, the concept of the heap
The so-called Heap, which is an array, can also be seen as an approximate complete binary tree. Each node in the tree corresponds to an element of the array. 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 any of the nodes, the value of the node is greater than the left child, the right child's value, but the child's value 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 represents the array length. Since the heap is built, the largest element of the array is stored at the root node a[1], and the largest element is moved back and sorted by swapping a[1] with the last element of the array. 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 maintain the maximum heap nature of the remaining array elements. The heap sorting algorithm, by repeating this process (n-1), enables the array to be sorted from small to large (because the largest heap is used). for a brief introduction to the two sub-functions mentioned above. function build_max_heap: Build a
heap . Because sub-array a (N/2+1..N) is the leaf node of the tree, no heap maintenance is required. Therefore, you can build the heap only if you need to maintain the A[1..N/2] array elements. function Max_heapify: the
maintenance heap. Procedure: Suppose A[i] represents a node of a tree, then A[2*i] is its left child,A[2*i+1] is its right child. Next, compare the size of the three to select the largest element subscript, deposited in the largest. Then, judge (largest==i), if not satisfied with the element exchange, the large elements move up. At this point, a subtree with a[largest] as the root node may not satisfy the nature of the heap, so it needs to be recursively called itself.
Third, the implementation of the algorithmThe above describes the heap implementation of array sorting principle, the following direct to the 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:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Heap sequencing (C language Implementation)