Insert sort--3 Hill Sort implementation

Source: Internet
Author: User

Hill sort is also known as narrowing the incremental sort, this sort method first groups the whole unordered sequence, set the size of each group is the grouping Factor DK. After the completion of the group, the first and the I+DK, I+2DK, i+3dk ... Element is a group. You can then sort the group in some way, using the insert sort.

After each group is ordered, a sequence of "ordered" sequences is obtained. Then reduce the value of the grouping Factor DK. To do this sort of thing again. The last sort is done until the DK is 1 o'clock.

This will sort out the entire sequence.

The following is an example of a code implemented using the interchange Sort method:

 Public voidShellsort (int[] ary) {             for(varDK = ary. Length/2; DK >=1; DK = DK/2)            {                //group the elements with a distance of DK to scan all groups                 for(inti = DK; i < ary. Length; i++)                {                    intj =0; inttemp = Ary[i];//The value that holds the current value, the corresponding position of the last group//Sort the group of elements with a distance of DK, for the first order, each group must have a maximum of 2 elements, which can be understood as a sort of exchange. //compare two values from the last set, forward, corresponding position, and actually insert sort                     for(j = i-dk; J >=0&& temp < ARY[J]; j = J-DK) {Ary[j+ DK] =Ary[j]; } ary[j+ DK] =temp; }            }        }

Insert sort--3 Hill Sort implementation

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.