1. Hill Sort--Shell Insertion Sort
Time Complexity: mathematicians are working hard to explore!
Applicable conditions: the improvement of the direct insertion sort, mainly for the reduction of the number of moves, depending on the value of the "incremental queue". Applicable to the situation with personal feelings, I do not know, anyway, I do not think I am a programmer, like to act on the Feeling.
1<?PHP2 $arr= [3,4,5,1,11,9,27,27,18,20];3 4 5 functionShellsort (Array&$arr,$dt)6 {7 //the improved version, which is related to the increment, is directly ordered, and when reversed, it can greatly reduce the number of Moves. 8 $straightInsertFunc=function(Array&$arr,$dt){9 $guild= 0;//Watch the Whistle .Ten Array_unshift($arr,$guild); one $arr=array_values($arr); a $len=Count($arr); - - for($i= 1 +$dt;$i<$len;$i+=$dt) the { - if($arr[$i-$dt] >$arr[$i]) - { - $arr[0] =$arr[$i]; + $arr[$i] =$arr[$i-$dt]; - for($j=$i+ T *$dt;$j> 0 &&$arr[$j] >$arr[0];$j-=$dt) + { a $arr[$j+$dt] =$arr[$j]; at } - $arr[$j+$dt] =$arr[0]; - } - } - Array_shift($arr); - }; in - if($dt<= 0) to { + Throw New Exception(' Param $dt error! '); - } the * for($i=$dt;$i>= 1; --$i) $ {Panax Notoginseng $straightInsertFunc($arr,$dt); - } the } + aShellsort ($arr, 3); the + Echo implode(‘,‘,$arr);
Operation Result:
PHP Insert Sort--hill Sort