The examples in this article describe the implementation principles and application methods of PHP heap sorting. Share it with everyone for your reference. The specific analysis is as follows:
Here, PHP is used as the description language to explain the principle of heap sorting in more detail. To ensure the readability of the program, no optimization is done. Some concepts about the heap in the PHP program are as follows:
Assuming n is the key of the current array, the parent node of n is n>>1 or n/2 (divisible); the left child node of n is l= n<<1 or l=n*2,n The right child node r=(n<<1)+1 or r=l+1
$arr=array(1,8,7,2,3,4,6,5,9);
The original structure of array $arr is as follows:
1
7
2 3 4 6
/
5 9
heapsort($arr);print_r($arr);
After sorting, the standard small top heap structure is generated as follows:
1
3
4 5 6 7
/
8 9
That is, array: array(1,2,3,4,5,6,7,8,9):
Copy code
I hope this article will be helpful to everyone’s PHP programming design.