Implementation principle and application method of PHP heap sequencing _php Skills

Source: Internet
Author: User

This paper describes the implementation principle and application method of PHP heap sequencing. Share to everyone for your reference. The specific analysis is as follows:

PHP as a description of the language described in detail in the heap sorting principle, due to ensure the readability of the program, so do not do optimization, PHP program on the heap of some of the concepts are as follows:

Assuming N is the key of the current array, the parent node of n is n>>1 or N/2 (divisible), and the left child node of n 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 morphological structure of the array $arr is as follows:

1
/
8 7
/         /
2 3 4 6
/
5 9
Heapsort ($arr);p rint_r ($arr);

The small top heap structure that generates a standard after sorting is as follows:

1
/
2 3
/       /
4 5 6 7
/
8 9
Both arrays: Array (1,2,3,4,5,6,7,8,9):

Copy Code code as follows:
Function Heapsort (& $arr)
{
Find the last element bit
$last =count ($arr);
$arr[0 is often ignored in heap sorting]
Array_unshift ($arr, 0);
Last Non-leaf node
$i = $last >>1;

Organize the large top heap, the largest number to the top of the heap, and the maximum number and the end of the heap, and in subsequent calculations ignore the maximum number of the back end of the array (last) until the top of the heap (last= heap top)
while (true)
{
Adjustnode ($i, $last, $arr);
if ($i >1)
{
Move the node pointer and traverse all non-leaf nodes
$i--;
}
Else
{
Critical last=1, both sorted complete
if ($last ==1) break;
When I is 1, each time the heap will get the maximum number (heap top, $arr [1]), repeat in the root node adjustment heap
Swap ($arr [$last], $arr [1]);
Keep the maximum number in size order at the end of the array and define the critical point last to avoid reordering the sorted elements after the array
$last--;
}
}
pops up the first array element
Array_shift ($arr);
}

Organize the current tree node ($n), the critical $last after the sorted elements
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, the parent node's right child is better than
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 a child node ($l) is exchanged with the value of the parent node ($n)
Swap ($arr [$l], $arr [$n]);
The value of the Swap Hofu node ($n) ($arr [$n]) may also be less than the value of the child node of the Atomic node ($l), so it is also necessary to adjust the child nodes of the Atomic node ($l), using recursion to implement the
Adjustnode ($l, $last, $arr);
}
}

Swap two values
Function swap (& $a,& $b)
{
$a = $a ^ $b;
$b = $a ^ $b;
$a = $a ^ $b;
}

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.