PHP Implementation heap Sequencing

Source: Internet
Author: User

$arr= [9, 43, 12, 0, 87, 1];functionSwap (&$arr,$i,$j){    $tmp=$arr[$i]; $arr[$i] =$arr[$j]; $arr[$j] =$tmp;}//Merge_sort ($arr);functionHeap_sort (&$arr){    $len=Count($arr)-1; $m=intval($len/2); //build a big top pile first     for($i=$m;$i>= 0;$i--) {Heap_adjust ($arr,$i,$len); }    //     for($i=$len;$i> 0;$i--){        Echo"$i"; Swap ($arr, 0,$i); Heap_adjust ($arr, 0,$i-1); }}/** $arr $start+1 to $end is a big top heap, the first element of the new addition, so that this element from the new to become a big top heap*/functionHeap_adjust (&$arr,$start,$end){    if($start>=$end){        return; }    $tmp=$arr[$start]; $i=$start;  for($j= 2 *$start;$j<=$end;$j*=2 ){        if($j+1 <=$end&&$arr[$j+1] >$arr[$j]){            $j++; }        if($arr[$j] <$tmp){             Break; }        $arr[$i] =$arr[$j]; $i=$j; }    $arr[$i] =$tmp;} Heap_sort ($arr);Var_export($arr);

PHP Implementation 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.