PHP Insert Sort--hill Sort

Source: Internet
Author: User

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

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.