"Sorting algorithm" hill sort principle and Java implementation __ Code

Source: Internet
Author: User
1. Basic idea:

Hill sort also becomes "shrinking incremental sort", the basic principle is that the array elements to be sorted are divided into several subsequence, so that the number of elements in each subsequence is relatively few, then the sequence of each subsequence is directly inserted, after the whole backlog sequence is "basically ordered", and then all the elements are immediately inserted into the order. Therefore, we want to adopt the strategy of jumping segmentation: to make a subsequence of a "increment" in order to ensure that the result of the direct insertion in the subsequence is basically orderly rather than local order. Hill sort is the optimization and upgrade of the direct insertion sort algorithm.
The so-called basic order, is the small keyword basic in front, large basic in the back, a moderate basic in the middle, such as {2,1,3,6,4,7,5,8,9,} can be called the basic order. But like {1,5,9,3,7,8,2,4,6}, 9 in third place, 2 in the third place is not basic order. 2, the complexity of the analysis:

Hill sort of the key is not casually grouped after their own sorting, but the interval between an "increment" of the record into a subsequence, to achieve a jump-type movement, so that the efficiency of the ranking improved. It is important to note that the last increment value of the increment sequence must be equal to 1. In addition, Hill sorting is not a stable sort algorithm because the record is a jump-type movement.
Hill ranking the best time complexity and average time complexity are, the worst time complexity of. 3, the sorting process is as follows:

Take the array {26, 53, 67, 48, 57, 13, 48, 32, 60, 50} as an example, the step sequence is {5,2,1}
Initialization keywords: [26, 53, 67, 48, 57, 13, 48, 32, 60, 50]

The final sort result:
4, Java implementation is as follows:

public static void Shellsortsmalltobig (int[] Data {int j = 0;
        int temp = 0; for (int increment = DATA.LENGTH/2, increment > 0; increment/= 2) {System.out.println ("increment:" + in
            Crement);
                for (int i = increment i < data.length; i++) {//System.out.println ("I:" + i);
                temp = Data[i];
                    for (j = i-increment J >= 0; J-= increment) {//System.out.println ("J:" + j);
                    SYSTEM.OUT.PRINTLN ("Temp:" + temp);
                    System.out.println ("data[" + j + "]:" + data[j]);
                    if (temp < data[j]) {data[j + increment] = Data[j];
                    } else {break;
            } data[j + increment] = temp;
        for (int i = 0; i < data.length i++) System.out.print (Data[i] + "");} public static void Main (string[] args) {int[] data = new int[] {26, 53, 67, 48, 57, 13, 48, 32, 60,
        50};
        Shellsortsmalltobig (data);
    System.out.println (arrays.tostring (data)); }

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.