This article mainly introduced the PHP implementation of the heap sorting algorithm, combined with an example of the PHP heap sequencing principle, implementation steps and related operation skills, the need for friends can refer to the next
Specific as follows:
heap is a generic term for a particular type of data structure in computer science, usually an array object that can be viewed as a tree .
heap {K1,k2,ki,..., kn} (ki <= k2i,ki <= k2i+1) | (ki >= k2i,ki >= k2i+1), (i = 1,2,3,4...N/2)
About Heaps:
The value of a node in the heap is always not greater than or less than the value of its parent node;
The heap is always a complete binary tree (below).
The largest heap of the root node is called the maximum heap or large heap, and the smallest heap of the root node is called the minimum heap or small Gan.
Complete binary Tree
When it comes to heap sequencing, it is impossible to mention the complete binary tree, these basic concepts are everywhere on the web, I picked the simplest one.
Complete binary tree: Except for the last layer, the number of nodes on each layer reaches the maximum, and on the last layer there are only a few junctions on the right.
I conclude myself that it is precisely because of the following two characteristics,
1. Only the last layer is allowed to have vacancy nodes and vacancies on the right, that is, the leaf node can only appear on the two layers with the highest level (the rules of storage);
2. If the parents of I>1,tree are Tree[i P 2] (the regularity of their parent-child node values);
To make it easy to sort.
Heap Sort
The heap is sorted in ascending order with a large top heap, and a tatsu sequence with a small top heap.
This example uses the small top heap of the Tatsu sequence to parse.
The heap sort steps are as follows:
1, we set the data (49, 38, 65, 97, 76, 13, 27, 50) to establish an array $arr;
2. Create a small top heap with an array of $arr (the main step, which is explained in the code comment, is the process of building a small top heap with an array);
3, the root of the heap (the smallest element) and the last leaf exchange, and the heap length minus one, jump to the second step;
4. Repeat 2-3 steps until there is only one node in the heap, and the sorting is complete.
PHP implementation of Heap ordering
Because it is an array, the subscript starts at 0, so the left dial hand node labeled N is 2n+1, the right child node is the 2n+2;//initialization value, and the initial heap $arr=array (49,38,65,97,76,13,27,50) is established; $arrSize =count ($ ARR);//Extract the first sort, because the last order does not need to exchange values again. Buildheap ($arr, $arrSize); for ($i = $arrSize-1; $i >0; $i-) {swap ($arr, $i, 0); $arrSize--; Buildheap ($arr, $arrSize);} Set the minimum heap function buildheap (& $arr, $arrSize) {//Calculate the first subscript $index, for the position of the number "97", compare the parent node and the child node of each subtree, and save the minimum value in the parent node//from $ The index compares a tree to form a minimum heap for ($index =intval ($arrSize/2)-1; $index >=0; $index-) {///If there is a left node, store its subscript in the minimum $min if ($index +1< $arrSize) {$min = $index *2+1; If there is a right sub-node, compare the size of the left and right nodes, if it is smaller, the subscript of its node is recorded in the minimum value $min if ($index *2+2< $arrSize) {if ($arr [$index *2+2]< $arr [$min]) { $min = $index *2+2; }}//Compare the smaller and parent nodes in the sub-nodes, Kawai nodes smaller, swap positions with the parent node, and update the smaller if ($arr [$min]< $arr [$index]) {swap ($arr, $min, $index); }}}}//This function is used to swap the data function swap (& $arr, $one, $another) {$tmp = $arr [$one] of the next array $arr labeled $one and $another; $arr [$one]= $arr [$another]; $arr [$another]= $tmp;}
Here is the final result of the sort:
Related recommendations:
Example analysis of PHP heap sorting algorithm
An example analysis of heap sorting algorithm in Java
PHP heap Sorting algorithm example detailed