PHP heap Sequencing implementation principle and application Method _php Tutorial

Source: Internet
Author: User

Implementation principle and application method of PHP heap sequencing


This article mainly introduced the PHP heap sequencing implementation principle and the application method, has analyzed the heap sorting principle and the use technique in detail, has the certain reference value, the need friend may refer to under

In this paper, the implementation principle and application method of PHP heap sequencing are described. Share to everyone for your reference. The specific analysis is as follows:

Here, PHP as a descriptive language to explain the heap sequencing principle, due to ensure the readability of the program, so do not optimize, the PHP program about the heap concepts are as follows:

Assuming N is the key of the current array, the parent of n is n>>1 or N/2 (divisible); The left child node of n is 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
Heapsort ($arr);p rint_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):

The code is as follows:

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;
}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/975889.html www.bkjia.com true http://www.bkjia.com/PHPjc/975889.html techarticle The implementation principle and application method of PHP heap sequencing This article mainly introduces the implementation principle and application method of PHP heap sequencing, and analyzes the principle of heap sequencing and its usage techniques in detail, and has a certain reference ...

  • 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.