Sort Algorithm Hill sort Java Implementation __ algorithm

Source: Internet
Author: User
Knowledge Preparation Basic Concepts

Hill sort: In the direct insertion sort based on the optimization, the direct insertion of the order in the N value of small when the efficiency is high, in the case of N very large if the ordering sequence is basically orderly, efficiency is still high, time efficiency can be promoted to O (n). Hill sort is also called narrowing the incremental sort.
1. Select an integer d (step) less than n, and then divide the sequence into Group D by step D, starting at the beginning of the first record with a group of D. Then the direct insertion in each group, after a trip, interval D sequence, with the improvement of order, reduce step D repeat. Until the d=1 makes the record of the interval 1 orderly, it reaches the whole ordered step decomposition

1. We have sequences 18, 24, 12, 15, 1, 27, 17, 19. We divide the sequence length by 2 as the first step. Each time after the original step on the basis of dividing by 2 rounding. Until the step is 1 after the direct insert sort.


As shown in the figure above, we divide the sequence into four groups according to the step 8/2=4, respectively:
[1,18], [24,27], [12,17], [15,19].
The final result is the direct insertion Order of each group:

1, 24, 12, 15, 18, 27, 17, 19

2. Continue to reduce the step size, in the previous step of the results Series 1, 24, 12, 15, 18, 27, 17, 19, based on the step to the previous step length divided by 2, that is, step to 4/2=2 repeat the first step as shown in the following figure.


The results are divided into two groups based on the step size, [1,12,17,18], [15,19,24,27], and then the direct insert sort is performed separately:

1, 15, 12, 19, 17, 24, 18, 27

3. We have seen the sequence gradually becoming an orderly process, knowing that the last step is 1, such as the current instance. The step to the third step is 2/2=1, so the last step of the direct insertion sequence is ordered.


To get the final sequence:

1, 12, 15, 17, 18, 19, 24, 27
Code Implementation
/**
     * Hill Sort
     *
     * @param num
     *
    /public static void Shellsort (int num[]) {
        int temp;
        The default step is the array length divided by 2
        int steps = num.length;
        while (true) {step
            = STEP/2;
            Determine the number of groupings
            for (int i = 0; i < step; i++) {
                //Insert the sort for the packet data directly
                (int j = i + step; j < num.length; j = J + Step) {
                    temp=num[j];
                    int k;
                   for (k=j-step;k>=0;k=k-step) {
                       if (num[k]>temp) {
                           num[k+step]=num[k];
                       } else{break
                           ;
                       }
                   Num[k+step]=temp
                }
            }
            if (step = = 1) {break
                ;
            }}
        }
    

Note: Some of the hill sort writes are slightly problematic on the web, and the direct insertion into the hill sort is bubbling.

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.