PHP Tutorial Heap Sequencing implementation principles and application code
Author:lajabs
email:agl0dhlvqgdtywlslmnvbq==
In this paper, PHP as a descriptive language in more detail on the principle of heap sequencing
Due to the readability of the program, no optimization is done.
Some concepts about heaps in PHP programs:
Assuming that n is the key of the current array
The parent node of n is n>>1 or N/2 (divisible);
Left child node of n l= n<<1 or L=n*2,n right child node r= (n<<1) +1 or r=l+1
*/
$arr =array (1,8,7,2,3,4,6,5,9);
/*
The original morphological structure of the array $arr is as follows:
1
/
8 7
/ /
2 3 4 6
/
5 9
*/
Heaps tutorial ort ($arr);
Print_r ($arr);
/*
After sorting, a standard small top heap structure is generated as follows:
1
/
2 3
/ /
4 5 6 7
/
8 9
Both arrays: Array (1,2,3,4,5,6,7,8,9)
*/
Function Heapsort (& $arr)
{
To find the last element bit
$last =count ($arr);
$arr[0 is usually ignored in heap sorting]
Array_unshift ($arr, 0);
Last Non-leaf node
$i = $last >>1;
Organize into a large top heap, the largest number to the top of the heap, and the maximum number and the end of the heap exchange, and in the subsequent calculation ignores the maximum number of array backend (last), until the heap top (last= heap top)
while (true)
{
Adjustnode ($i, $last, $arr);
if ($i >1)
{
Move node pointer, traverse all non-leaf nodes
$i--;
}
Else
{
Critical point Last=1, both sorted and completed
if ($last ==1) break;
When I is 1, it means that each heap will get the maximum number (heap top, $arr [1]), and repeatedly adjust the heap at the root node
Swap ($arr [$last], $arr [1]);
Keep the maximum number in size order at the end of the array and define the last point to avoid re-cluttering the sorted elements behind the array as you defragment the heap
$last--;
}
}
pops up the first array element
Array_shift ($arr);
}
Organize the current tree node ($n), and $last the sorted elements after the critical point
function Adjustnode ($n, $last,& $arr)
{
$l = $n <<1; $n's left child bit
if (!isset ($arr [$l]) | | $l > $last) return;
$r = $l +1; $n's right child bit
If the right child is larger than the left child, let the right child of the parent node compare
if ($r <= $last && $arr [$r]> $arr [$l]) $l = $r;
If the child node is $n larger than the parent node, the $n is exchanged with the parent node
if ($arr [$l]> $arr [$n])
{
The value of the child node ($l) is exchanged with the value of the parent node ($n)
Swap ($arr [$l], $arr [$n]);
The value of the Interchange stepfather Node ($n) ($arr [$n]) may also be less than the value of the child nodes of the Atomic node ($l), so it is also necessary to adjust the child nodes of the Atomic node ($l), with recursive implementation
Adjustnode ($l, $last, $arr);
}
}
Exchange two values
Function swap (& $a,& $b)
{
$a = $a ^ $b; $b = $a ^ $b; $a = $a ^ $b;
}
http://www.bkjia.com/PHPjc/444907.html www.bkjia.com true http://www.bkjia.com/PHPjc/444907.html techarticle PHP Tutorial Heap Sequencing implementation principles and application code Author:lajabs email:agl0dhlvqgdtywlslmnvbq== This article explains the heap sorting principle in more detail with PHP as the description language for ensuring program readability ...