Hill sort (Shell sort)

Source: Internet
Author: User

1. Definition

The Hill sort (Shell sort) is a sort of insertion. Because of D. L The shell was named after it was introduced in 1959.

2. Basic Ideas

First, take an integer less than n D1 as the first increment, dividing all the records of the file into D1 groups. All records that are multiples of the DL are placed in the same group. First, the direct interpolation in each group is sorted, then the second increment d2<d1 repeats the above grouping and sorting until the increment dt=1 (DT<DT-L<...<D2<D1) is taken, that is, all records are placed in the same group for direct insert sorting.

This method is essentially a grouping insertion method.

3, Shell sorting algorithm implementation(1) algorithm description without monitoring whistle
  void Shellpass (seqlist r,int D)   {//Hill sort in order, D for current increment for     (i=d+1;i<=n;i++)//Will r[d+1: N] Insert each group's current ordered area       if (r[i].key<r[i-d].key) {         r[0]=r[i];j=i-d;//r[0] Only the staging unit, not         the insertion position            of Sentinel do {//find R[i] R[J+D];=R[J];///////Post            -record j=j-d;//Find Previous record         }while (j>0&&r[0].key<r[j].key);         r[j+d]=r[0];//Insert R [i] to the correct position       }//endif   }//shellpass  void  shellsort (seqlist R)   {    int increment=n;// Incremental initial value, you may wish to set n>0 do    {          increment=increment/3+1;///To seek the next increment          shellpass (r,increment); A trip to the increment shell insert sort       }while (increment>1)    }//shellsort

Attention:

When incremental d=1, Shellpass and Insertsort are basically the same, just because there is no sentinel and in the inner Loop added a cycle to determine the condition "j>0" in order to prevent subscript out of bounds.

(2), the shell sorting algorithm based on the monitoring whistle4. Algorithm Analysis(1), the selection of incremental sequence

The execution time of the shell sort depends on the increment sequence.

Common characteristics of a good delta sequence:

① the last increment must be 1;

② should try to avoid cases in which the values in the sequence (especially the neighboring values) are multiplied by each other.

A lot of experiments have been done to give a better result: when n is larger, the number of comparisons and movements is between nl.25 and 1.6n1.25.

(2), the time performance of shell sorting is better than direct insertion sort

The time performance of hill sorting is better than the reason for direct insertion sorting:

① the number of comparisons and moves required to insert a sort directly when the initial state of the file is basically ordered is low.

② when the n value is small, the difference between N and N2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity of 0 (N2) is not very different.

③ in the beginning of the hill, the increment larger, more groups, the number of records in each group is small, so the direct insertion within the group is faster, and then the incremental di gradually reduced, and the number of groups gradually decreased, and the group of records gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the orderly state So the new trip sort process is also faster.

Therefore, the efficiency of hill sort is better than direct interpolation.

(3), stability

The hill sort is not stable. In the example above, the relative order of the two identical keyword 49 before and after the order has changed.

Hill sort (Shell sort)

Related Article

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.