Php heap sorting implementation principle and application code _ PHP Tutorial

Source: Internet
Author: User
Php heap sorting implementation principle and application code. Php Tutorial heap sorting implementation principle and application code author: lajabsemail: agl0dhlvqgdtywlslmnvbq this article uses php as the description language to explain in detail the heap sorting principle to ensure program readability. 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;
}


Export author: lajabs email: agl0dhlvqgdtywlslmnvbq = This article uses php as the description language to explain in detail the heap sorting principle to ensure program readability...

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.