Algorithm exercises--Direct insert sort, hill sort

Source: Internet
Author: User

Background:

  originally although has learned the data structure, sorts some, but did not have the good studious, now review .

Business:

  1. Direct Insert Sort

    Go directly to the encyclopedia: each time you take out a number in an unordered array, compare it to the number in the ordered array, and plug it into place until the unordered table is empty and the ordered table is full.

The principle is very simple, as far as I am concerned, the first thought is to create a new new_list array, act as an ordered table, and then step by step the values of the unordered table are taken out into the new array. But, after writing, read the book on the example, the above directly with the singular group can also, virtual division of the array into two parts. The code is much simpler. So I also modified my own code.

    The code is as follows:

<?PHPfunctionStr_i_sort ($list){    Array_unshift($list, 0);  for($i= 1;$i<Count($list);$i++)    {        $list[0] =$list[$i]; $j=$i-1;  while($j> 0 &&$list[0] <$list[$j])        {            $list[$j+1] =$list[$j--]; }        $list[$j+1] =$list[0]; }    Array_shift($list); return $list;}$list=Array(36,25,48,12,65,43,20,36);Print_r(Str_i_sort ($list) ;//array ([0] [+] [1] = [2] [3] = [4] = [5] + [6] = [7] [+] = 48] ?>

Where an array is added with an list[0] in order to store the value that is currently being inserted, and the value is overwritten when the arrays move backwards. The encyclopedia says, you can use list[0] to control the array out of bounds, but I have no particular feeling.

  2. Hill sort

    principle: First take an integer less than n D1 as the first increment, grouping the data. All records with a multiple of D1 are placed in the same group. First, the direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the above groupings and sorts until the increment =1 ( < ... <d2<d1) is taken. That is, all records are placed in the same group for direct insert sorting. (Incremental sequence values are a problem to be studied, but for simplicity, use =/2)

The hill sort is actually an improvement on the direct insertion sort, and the more efficient it is to directly insert the sort if the given sequence is closer to the order. Therefore, by using this grouping method, each group is ordered first, until the final order is complete. One of the things I think is the trouble is how to insert each group directly into the sort. The first thought was that each group was pulled out of a separate row, but found it was not feasible. So we looked at the implementation method, and found that all were alternating and replaced in the order of the original sequence subscript.

    The code is as follows:

<?PHPfunctionShell_sort ($list){        $count=Count($list); $step= Floor($count/2);//Step Size     while($step> 0)    {         for($i=$step;$i<$count;$i++)        {            $j=$i-$step;  while($j>=0 &&$list[$j] >$list[$j+$step])            {                $tmp=$list[$j]; $list[$j] =$list[$i]; $list[$i] =$tmp; $j=$j-$step; }        }        $step= Floor($step/2); }    return $list;}$list=Array(165,12316,123,496,1265,123,456,1515);Print_r(Shell_sort ($list));//Array ([0] = 123 [1] = 123 [2] = 165 [3] = 456 [4] = = 496 [5] = = 1265 [6] = = 1515 [7] = = 12316)?>

Over These days are ready to take a look at the sorting system, and then go to the fun topic.

Algorithm exercises--Direct 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.