Heap Sort
Heap sequencing (Heapsort) is also a relatively efficient sorting method, with the time complexity of heap ordering being O (n lgn), and heap ordering using a data structure called a heap for management.
Two-fork Pile
The binary heap is a special heap, the binary heap is a complete binary tree or is an approximate complete binary tree. The binary heap satisfies the heap characteristic: the parent node's key value always maintains a fixed order relation to the key value of any one child node, and the Saozi right subtree of each node is a binary heap.
As shown, (a) is a binary heap (maximum heap) and (b) is the storage form of this binary heap in the array.
By giving the subscript I of a node, it is easy to calculate the subscript of the parent node and the left and right child nodes, and for convenience, the subscript in the binary heap is calculated from 1.
The pseudo code is as follows:
PARENT(i) // 得到父节点的下标,结果向下取整 return i/2LEFT(i) // 得到左子节点的下标 return2iRIGHT(i) // 得到右子节点的下标 return2i+1
On most computers, you can quickly get results with a displacement operation:
- Left operation: i shift one position
- Right : I shift one bit to the left and add 1 to the lowest bit
- Parent Action: I shift one bit to the right
And these three functions are best implemented in the form of macros or inline functions, such as using macros in the C language:
#definePARENT>>1 // 取得父节点的下标#define<<1 // 取得左边子节点的下标#define<<1+1 // 取得右边子节点的下标
Classification of binary piles
Binary stacks are divided into two categories: maximum heap and minimum heap
The maximum heap must satisfy the nature in addition to the root node:
A[parent (i)] >= A[i]
The minimum heap must satisfy the nature in addition to the root node:
A[parent (i)] <= A[i]
In the heap sorting algorithm we are using the maximum heap
Heap Sorting algorithm
The heap sorting algorithm is mainly applied to three functions:
void maxHeapify(intint); // 用于维护堆的性质void bulidMaxHeap(int []); // 建堆void heapSort(int []); // 堆排序
maxheapify function
The function is to maintain the nature of the heap, its first parameter is an array of data, and the second parameter is the subscript of a node. The implementation of this function is as follows:
void maxHeapify ( Span class= "Hljs-keyword" >int nums[], int i) { L = left (i); //gets the subscript of the left child node int r = Right (i); //get subscript for right child node int largest; //the index of the largest value in the current node and the left and right two child nodes if (l <= heapsize && nums[l] > nums[i]) largest = l; else largest = i; if (R <= heapsize && Nums[r] > nums[largest]) largest = R; if (Largest! = i) {Swap (nums+i, nums+largest); Maxheapify (Nums, largest); }}
This function realizes the function of maintaining the nature of the heap, first find out the current node and its left and right child nodes of the three nodes of the largest node, if the current node is the largest node to end the program, or exchange the current node and the maximum value of three nodes, after the exchange of the node continues to judge, Until the condition is met or the node is a leaf node.
The process when executing maxheapify (nums, 2)
Bulidmaxheap function
The purpose of this function is to turn the normal array into a binary heap, as follows in the C language implementation:
void bulidMaxHeap(int nums[]){ int i; for(i = MAX_N/21; i--){ maxHeapify(nums, i); }}
From the bottom up, the first non-leaf node from right to left starts calling the Maxheapify function to maintain the binary heap
Heapsort function
This function implements the sorting function, and the following is the implementation of the C language:
void heapSort(int nums[]){ bulidMaxHeap(nums); int i; heapSize = MAX_N; for2; i--){ 1, nums + i); heapSize--; 1); }}
Depending on the nature of the maximum heap, the root node is always the largest number in the entire heap, so the last leaf node and root node in the current binary heap are exchanged, and the last node is deleted from the two-fork heap after the exchange is completed, which is the heapsize– operation, and then the two-fork heap is maintained after switching with maxheapify, The sorting is done by looping to only one node left.
Basically the heap sort is like this, and finally the heap sort of the dynamic graph and the complete C language code
A demonstration of the heap sorting algorithm. First, the elements are re-queued to match the conditions of the heap. The structure of the heap tree is simply plotted before the sorting process in the diagram.
Complete C-Language implementation code:
#include <stdio.h>#include <stdlib.h>#include <time.h>#define MAX_N#define Parent (i) I >> 1 //Get subscript for parents #define LEFT (i) I << 1 //Get Subscript on the child node #define RIGHT (i) (I << 1) + 1 //Get subscript of sub-node voidSwapint*,int*);//Exchange functionvoidMaxheapify (int[],int);//For maintaining the nature of the heapvoidBulidmaxheap (int[]);//Jian YuvoidHeapsort (int[]);//Heap sortintHeapSize;//Heap size, self-decrement when sortingintMain () {intNums[max_n +1];//Here subscript from the beginning intI HeapSize = Max_n; Srand (Time (0));printf("Original array:"); for(i =1; I <= max_n; i++) {Nums[i] = rand ()% Max_n;printf("%d\t", Nums[i]); }printf("\ n"); Bulidmaxheap (Nums);printf("After Bulid heap:"); for(i =1; I <= max_n; i++) {printf("%d\t", Nums[i]); }printf("\ n"); Heapsort (Nums);printf("After heap sort:"); for(i =1; I <= max_n; i++) {printf("%d\t", Nums[i]); }printf("\ n");}voidSwapint*a,int*B) {inttemp = *a; *a = *b; *b = temp;}voidMaxheapify (intNums[],inti) {intL = left (i);//Get subscript for left child node intR = Right (i);//Get subscript for right sub-node intlargest;Subscript of the largest value in the current node and the left and right two child nodes if(l <= heapsize && nums[l] > nums[i]) largest = l;Elselargest = i;if(R <= heapsize && Nums[r] > nums[largest]) largest = R;if(Largest! = i) {Swap (nums+i, nums+largest); Maxheapify (Nums, largest); }}voidBulidmaxheap (intNums[]) {intI for(i = max_n/2; I >=1; i--) {maxheapify (nums, i); }}voidHeapsort (intNums[]) {bulidmaxheap (nums);intI HeapSize = Max_n; for(i = max_n; I >=2; i--) {Swap (nums +1, Nums + i); heapsize--; Maxheapify (Nums,1); }}
[Algorithm learning note] Sorting algorithm--heap sort