Heap sort: What is a heap? What is the biggest heap? What is a binary heap? What is the heap sorting algorithm? How does php implement heap sequencing?

Source: Internet
Author: User

This document tags: heap sort php php algorithm heap sorting algorithm two fork heap data structure /c12> REST server

What is a heap

Here the heap (binary heap), refers to not the stack of the heap, but a data structure.

The heap can be regarded as a complete two-fork tree, and an "excellent" property of a complete binary tree is that each layer is full except the bottommost, which allows the heap to be represented by an array, with each node corresponding to an element in the array.

The relationship between the array and the heap

Binary heaps are generally divided into two types: maximum heap and minimum heap.

What is the maximum heap

The element value of each parent node in the heap is greater than or equal to its child node (if present), so the heap is a maximum heap

Therefore, the largest element value in the maximum heap appears at the root node (heap top).

Relationship between node and array index

For a given node subscript I, it is easy to calculate the parent node of the node, the child node subscript, and the calculation formula is very beautiful and simple

The second block, how to adjust the heap to the maximum heap, this part is the focus

The whole process is as shown

In this small heap of 4,14,7, the parent node 4 is smaller than the left child 14, so the two exchange

In 4,2,8 this small heap inside, the parent node 4 small less right operand child 8, so the two exchange

Shows the process of adjustment, which is implemented recursively until the maximum heap is adjusted.

Third piece, heap sort introduction

The heap sort is to take the maximum number out of the top of the heap,

The remaining heap continues to be adjusted to the maximum heap, and the process is introduced in the second block, which is implemented recursively

After the remainder is adjusted to the maximum heap, the maximum number of heap tops is removed again, the remainder is adjusted to the maximum heap, and the process lasts until the remaining number is only one end

The bottom three graphs describe the whole process in detail

Specific PHP implementations :

/**

 * 使用异或交换2个值,原理:一个值经过同一个值的2次异或后,原值不变

 * @param int $a

 * @param int $b

 */

functionswap(&$a,&$b){

    $a= $a^$b;

    $b= $a^$b;

    $a= $a^$b;

}

/**

 * 整理当前树节点($n),临界点$last之后为已排序好的元素

 * @param int $n

 * @param int $last

 * @param array $arr

 *

 */

functionadjustNode($n,$last,&$arr){

    $l= $n<<1;   // 左孩子

    if( !isset($arr[$l])||$l>$last){

        return;

    }

    $r= $l+1;  // 右孩子

    // 如果右孩子比左孩子大,则让父节点与右孩子比

    if( $r<=$last&&$arr[$r]>$arr[$l] ){

        $l= $r;

    }

    // 如果其中子节点$l比父节点$n大,则与父节点$n交换

    if( $arr[$l]>$arr[$n] ){

        swap($arr[$l],$arr[$n]);

        // 交换之后,父节点($n)的值可能还小于原子节点($l)的子节点的值,所以还需对原子节点($l)的子节点进行调整,用递归实现

        adjustNode($l, $last, $arr);

    }

}

/**

 * 堆排序(最大堆)

 * @param array $arr

 */

functionheapSort(&$arr){

    // 最后一个蒜素位

    $last= count($arr);

    // 堆排序中常忽略$arr[0]

    array_unshift($arr, 0);

    // 最后一个非叶子节点

    $i= $last>>1;

    // 整理成最大堆,最大的数放到最顶,并将最大数和堆尾交换,并在之后的计算中,忽略数组最后端的最大数(last),直到堆顶(last=堆顶)

    while(true){

        adjustNode($i, $last, $arr);

        if( $i>1 ){

            // 移动节点指针,遍历所有节点

            $i--;

        }

        else{

            // 临界点$last=1,即所有排序完成

            if( $last==1 ){

                break;

            }

            swap($arr[$last],$arr[1]);

            $last--;

        }

    }

    // 弹出第一个元素

    array_shift($arr);

}

is written at the end: for Freedom look outside the world, and it this line, not to go to Google data, finally, Amway a V--PN agent. a red apricot accelerator , to Google data is the absolute first choice, fast connection, easy to use. I bought 99¥ for a year, through this link (http://my.yizhihongxing.com/aff.php?aff=2509 Registered after the Member center to lose the coupon code, split down, only 7 dollars a month, special benefits.

This document tags: heap sort php php algorithm heap sorting algorithm two fork heap data structure REST server

Turn from SUN's BLOG-focus on Internet knowledge, share the spirit of the Internet!

Address: Heap Sort: What is a heap? What is the biggest heap? What is a binary heap? What is the heap sorting algorithm? How does php implement heap sequencing? "

related reading:" I am a G powder, has been concerned about Google, recently Google has some little gestures, probably a lot of people do not understand "

related reading:" machine learning leads to technological innovation in the field of cognition, so how can the SaaS industry be changed by machine learning?" "

related reading:"VPS Tutorial Series: DNSMASQ + DNSCrypt + SNI Proxy smooth access to Google configuration tutorial "

Related reading: useful for programmers: 2017 latest in Google's Hosts file download and summary of the various hosts encountered the problem of the solution and configuration of the detailed

Related reading: Aaron swartz– The internet genius of the life course: every moment asked himself, now the world what is the most important thing I can participate in doing? "
Related reading: " site environment Apache + PHP + mysql xampp, how to implement a server on the configuration of multiple sites?" "

Related reading: What is the engineer culture? Why are the engineers alive? As an IT or internet company Why should the engineer text

related reading: the Win10 perpetual activation tutorial and how can I see if the Windows system is permanently activated? 》

Related blog:SUN ' S blog -Focus on Internet knowledge and share the spirit of Internet! Go and see:www.whosmall.com

Original address: http://whosmall.com/?post=244

Heap sort: What is a heap? What is the biggest heap? What is a binary heap? What is the heap sorting algorithm? How does php implement heap sequencing?

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.