Php tutorial heap sorting implementation principle and application code
Author: lajabs
Email: agl0dhlvqgdtywlslmnvbq =
This article uses php as the description language to explain in detail the heap sorting principle
Optimization is not performed to ensure program readability.
Some concepts about heap in php programs:
Assume that n is the key of the current array.
The parent node of n is n> 1 or n/2 (Division );
N left subnode l = n <1 or l = n * 2, n right subnode r = (n <1) + 1 or r = l + 1
*/
$ Arr = array (, 9 );
/*
The original structure of the array $ arr is as follows:
1
/
8 7
//
2 3 4 6
/
5 9
*/
Heaps tutorial ort ($ arr );
Print_r ($ arr );
/*
The standard small top Heap Structure generated after sorting is as follows:
1
/
2 3
//
4 5 6 7
/
8 9
Array: array (1, 2, 3, 4, 5, 6, 7, 8, 9)
*/
Function heapsort (& $ arr)
{
// Find the last element bit
$ Last = count ($ arr );
// $ Arr [0] is usually ignored in heap sorting.
Array_unshift ($ arr, 0 );
// The last non-leaf node
$ I = $ last> 1;
// Organize the heap into a large top heap, increase the maximum number to the top of the heap, switch the maximum number to the end of the heap, and ignore the maximum number (last) of the array backend in subsequent calculations ), until the heap top (last = heap top)
While (true)
{
Adjustnode ($ I, $ last, $ arr );
If ($ I> 1)
{
// Move the node pointer to traverse all non-leaf nodes
$ I --;
}
Else
{
// Critical point last = 1, which indicates that all sorting is completed.
If ($ last = 1) break;
// When I is 1, the maximum number (heap top, $ arr [1]) will be obtained for each heap arrangement, and the heap will be adjusted repeatedly at the root node.
Swap ($ arr [$ last], $ arr [1]);
// Retain the maximum number at the end of the array in order of size, and define the critical point last to avoid disrupting the elements sorted after the array During Heap sorting.
$ Last --;
}
}
// The first array element is displayed.
Array_shift ($ arr );
}
// Organize the current Tree node ($ n). Elements sorted after the critical point $ last
Function adjustnode ($ n, $ last, & $ arr)
{
$ L = $ n <1; // left child location of $ n
If (! Isset ($ arr [$ l]) | $ l> $ last) return;
$ R = $ l + 1; // The right child of $ n
// If the right child is larger than the left child, compare the right child of the parent node
If ($ r <= $ last & $ arr [$ r]> $ arr [$ l]) $ l = $ r;
// If the child node $ l is larger than the parent node $ n, it is switched to the parent node $ n.
If ($ arr [$ l]> $ arr [$ n])
{
// Exchange the value of the child node ($ l) with the value of the parent node ($ n)
Swap ($ arr [$ l], $ arr [$ n]);
// The value ($ arr [$ n]) of the parent node after the switch may be smaller than the value of the child node of the atomic node ($ l, therefore, we need to adjust the subnodes of an atomic node ($ l) and implement them recursively.
Adjustnode ($ l, $ last, $ arr );
}
}
// Exchange two values
Function swap (& $ a, & $ B)
{
$ A = $ a ^ $ B; $ B = $ a ^ $ B; $ a = $ a ^ $ B;
}