PHP Insert Sort--binary find

Source: Internet
Author: User

1. Binary Lookup-Binary insertion Sort

Complexity of Time: O (n^2)

Applicable conditions: Relatively direct insertion of the sort, reducing the number of comparisons. Suitable for situations where there is less digital than the sort required.

1<?PHP2 namespace Test;3 4 $a= [0 =>3,4,5,1,11,9,27,27,18,20];5 6 functionBinaryinsertsort (Array&$arr)7 {8     $guild= 0;//Watch the whistle .9     $arr=array_values($arr);Ten     Array_unshift($arr,$guild); One     $len=Count($arr); A  -      for($i= 2;$i<$len;$i++) -     { the         if($arr[$i-1] >$arr[$i]) -         { -             $arr[0] =$arr[$i]; -             $low= 1; +             $high=$i-1; -  +              while($low<=$high) A             { at                 $mid= ($high+$low)/2; -                 if($arr[$mid] >$arr[0]) -                 { -                     $high=$mid-1; -                 } -                 Else in                 { -                     $low=$mid+ 1; to                 } +             } -  the              for($j=$i;$j>=$high+ 1; --$j) *             { $                 $arr[$j] =$arr[$j-1];Panax Notoginseng             } -  the             $arr[$high+ 1] =$arr[0]; +         } A     } the     Array_shift($arr); + } -  $Binaryinsertsort ($a); $ Echo implode(‘,‘,$a);

Output:

PHP Insert Sort--binary find

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.