Detailed explanation of heap sorting algorithm implemented by PHP, and detailed explanation of heap Sorting Algorithm
This example describes the heap sorting algorithm implemented by PHP. We will share this with you for your reference. The details are as follows:
Experience
After I got a job, I was overwhelmed by the technical skills when I interviewed this company, because my data structure and other basics were too poor, although I wanted to be a designer... However, I can come to my internship for the sake of PHP writing, but I am still determined to fill in the foundation. In fact, I did feel the importance of the foundation before. Some deep things are at the bottom layer and cannot be done without learning well. For example, when I used PHP for websocket, I involved the concepts of data packets and data frames. I was confused that even data could not be processed, and I had to complete it later. So I am going to learn about the basic knowledge of data structures, algorithms, networks, and so on. I am also here to remind you, don't turn around like me, it's even late to understand.
Today, I was asked about the heap sorting. When I was asked, I forgot the concept of a full binary tree. Fortunately, I still have a little bit of data structure, and I have understood some of the data. So I want to use PHP to write the binary tree heap sorting. By the way, I also want to review the binary tree, heap and other data structures.
Heap
Heap is a general term for a special data structure in computer science. It is usually an array object that can be seen 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 heap:
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 largest heap or large root heap, and the smallest heap of the root node is called the smallest heap or small root heap.
Full Binary Tree
When talking about heap sorting, we can't help but mention the full binary tree. These basic concepts are everywhere on the Internet. I picked the simplest one ..
Complete Binary Tree: Except for the last layer, the number of nodes on each layer reaches the maximum value. On the last layer, only a few nodes on the right are missing.
I personally think that it is precisely because of the following two features,
1. Only vacant nodes on the last layer can be left on the right, that is, the leaf node can only appear on the maximum two layers of the hierarchy (Regular storage mode );
2. If I> 1, the tree's parent is tree [I div 2] (the regularity of its parent and child node values );
It makes sorting very convenient.
Heap sorting
Heap sorting uses a large top heap for ascending order and a small top heap for descending order.
In this example, the small top heap in descending order is used for parsing.
The heap sorting steps are as follows:
1. We create an array $ arr for data (49, 38, 65, 97, 76, 13, 27, and 50;
2. Use the array $ arr to create a small top heap (the main step is to explain in the code comment that the process of creating a small top heap with an array );
3. Swap the heap root (minimum element) with the last leaf, and subtract the heap length by one and jump to the second step;
4. Repeat steps 2-3 until there is only one node in the heap and the sorting is completed.
PHP implementation of heap sorting
// Because it is an array and the subscript starts from 0, the left subnode of the subscript n root node is 2n + 1, and the right subnode is 2n + 2; // initialization value, create an initial heap $ arr = array (,); $ arrSize = count ($ arr); // extract the first sort, because the last sorting does not need to be exchanged. BuildHeap ($ arr, $ arrSize); for ($ I = $ arrSize-1; $ I> 0; $ I --) {swap ($ arr, $ I, 0 ); $ arrSize --; buildHeap ($ arr, $ arrSize);} // use an array to create the minimal heap function buildHeap (& $ arr, $ arrSize) {// calculate the initial subscript $ index, which is the position of the number "97" and compares the Parent and Child Nodes of each subtree, store the minimum value in the parent node // cyclically compare a tree from $ index to form the minimum heap for ($ index = intval ($ arrSize/2)-1; $ index> = 0; $ index --) {// if there is a left node, store its subscript to the minimum value $ min if ($ index * 2 + 1 <$ arrSize) {$ min = $ index * 2 + 1; // if there is a right subnode, compare the size of the left and right nodes. If the right subnode is smaller, add the subscript of the node to the minimum value $ min if ($ index * 2 + 2 <$ arrSize) {if ($ arr [$ index * 2 + 2] <$ arr [$ min]) {$ min = $ index * 2 + 2 ;}} // compare the child node with the parent node. If the child node is small, it exchanges the location with the parent node, updated if ($ arr [$ min] <$ arr [$ index]) {swap ($ arr, $ min, $ index );}}}} // This function is used to swap the data function swap (& $ arr, $ one, $ another) labeled $ one and $ another in the $ arr) {$ tmp = $ arr [$ one]; $ arr [$ one] = $ arr [$ another]; $ arr [$ another] = $ tmp ;}
The final result of sorting is as follows: