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