Sort of hill sort (shell sort)

Source: Internet
Author: User

Objective

  This blog is based on the blog of the Brothers, the blog address click on it can go in, there are many good blog, my sorting algorithm is from this; some of the concepts of data architecture I will not elaborate on, Wu fan's blog is explained in detail, and I write these blog just to record their learning process, Added some of their own understanding, but also want to provide help to others.

Premise Story

Sao year in the last with the blogger after the direct insertion of the sort of discussion, found the blogger, said: "Bo Master, for direct insertion sort, I have a major discovery," Bo Master thought, asked: "What found?" ", Sao Year:" I found the following two points "

    1) When the number of sequences is relatively small, direct insertion sorting efficiency is high ; this good understanding, the number is less, then the number of inserts is less, Bo said: "Well, this discovery is not difficult, but also need careful."

    2) If the sequence itself is basically orderly, then the direct insertion sorting efficiency is high ; Blogger: "Eh?" "You see the core code of the direct insertion sort," explained the Sao year: "

 for (int i=1; i<len; i++) {                                        for (int j=i-1; j>=0&&arr[j]>arr[j+1 ]; j--) {                        swap (arr,j,j+1);            }        }

The year goes on: "If the sequence is orderly, then the j>=0&&arr[j]>arr[j+1] condition is not satisfied, the insert operation will not be executed, the efficiency is naturally high." ”

Bo master: "then?" ”。

SAO years: "So can we do something on these two points to improve the efficiency of the direct insertion sort on the normal sequence?" ”。

The above two conditions are too harsh, the actual record of less or basic order is a special situation, there are conditions of course is good, conditions do not exist, we create conditions, but also can do; we can group the series, divide into several sub-sequences, Then each sub-sequence is directly inserted into the order, when the whole sequence is basically ordered, the attention is only basic order, and then a direct insertion of the whole record to sort .

There must have been some confusion at this point. This is not right, for example, we now have the sequence is {5,3,7,9,1,6,4,8,2}, now divides it into three groups, {5,3,7}, {9,1,6},{4,8,2}, even if they are sorted out, into {3,5,7},{1,6,9},{2,4,8}, Then merge them into {3,5,7,1,6,9,2,4,8}, at this time, this sequence is still disorderly, not basically ordered, to sort or to repeat the direct insertion in order, this is useful? Need to emphasize, the so-called basic order, is the small keyword basic in front, large basic in the back, the basic in the middle, like {2,1,3,6,4,7,5,8,9} This can be called basic order. But like {3,5,7,1,6,9,2,4,8} such as 7 in third place, 2 in the third place in the bottom is not basically orderly.
So the question is, the purpose of dividing the records to be sorted is to reduce the number of records to be sorted, and to develop the whole sequence to a basic order. And as the above, after the group, the method of the respective sorting does not reach our requirements. Therefore, we need to take the leap segmentation strategy: To make a sub-sequence of records that are separated by an "increment" so as to ensure that the results obtained by direct insertion in the subsequence are basically ordered rather than partially ordered .

Basic ideas

The entire sequence is split by a certain "increment", then directly into the sequence of sub-sequences, so that the resulting results are basically ordered, and finally the basic ordered sequence of a direct insertion of the order, so that the whole sequence

Code implementation

Java implementation

       /*** Hill Sort *@paramarr Destination Sequence*/     Public Static voidShellsort (int[] arr) {        intLen =arr.length;  for(intGAP=LEN/2; gap>=1; GAP=GAP/2) {//splits the entire sequence, where the element spacing is gap (that is, increment)//Direct Insert sort for a sub-sequence             for(inti=gap+1; i<len; i++){                 for(intJ=i-gap; j>=0&&arr[j]>arr[j+gap]; j=j-Gap) {Swap (Arr,j,j+gap); }            }        }    }    
View Code

Performing process Simulations

1) The program begins execution with an initial sequence of {5,3,7,9,1,6,4,8,2}, such as:

2) Initial gap=len/2=4

2.1) i=gap=4, initial j=0; compare Arr[j] with arr[j+gap], i.e. arr[0] and arr[4], such as:

J=j-gap=-4, jump, i++,i=5.

2.2) I=5,j=i-gap=1,arr[1]=3 < arr[5]=6, do not exchange data, such as:

    

J=j-gap=-3, jump, i++,i=6.

2.3) Similarly, when i=6,7, the sequence is as follows:

2.4) When i=8, the sequence is as follows:

Then gap=4, the final order is listed as

3) gap=gap/2=2

3.1) I=gap=2,j=i-gap=0,arr[0]=1 < arr[2]=4 do not swap, j=j-gap=-2,i++, this time the sequence is:

3.2) I=3,j=i-gap=1,arr[1]=3 < arr[3]=8, no Exchange, j=j-gap=-1,i++, this time the sequence is:

3.3) Similarly, when i=4:

3.4) I=5:

3.5) I=6:

3.6) I=7:

3.7) I=8:

4) Gap=gap/2=1, at this time

 for (int gap=len/2; gap>=1; gap=gap/2) {                    // split entire sequence, element spacing is gap (that is, increment            )// sub-sequence for direct insert sort for            (  int I=gap; i<len; i++) {                for (int j=i-gap; j>=0&&arr[j]>arr[j+gap];j=j-Gap) {                            Swap (arr,j,j+gap);                }            }

It is

            // Direct Insert Sort            for a sub-sequence  for (int i=1; i<len; i++) {                for (int j=i-1; j>=0&&arr[j]>arr[j+1 ]; j=j-1) {                            swap (arr,j,j+1);                }            }

I believe we all found that the above code is our direct insertion sort, then the specific simulation process I will not repeat, do not understand can go to see the sort of direct insertion sort

At this point, the entire sequence is ordered.

A difficult place to understand

  Through the analysis of this code, I believe that we have some understanding that the key to the hill is not a random grouping after their respective sorting, but will be separated by an "increment" of the record to form a sub-sequence, to achieve a jump-type movement, so that the efficiency of the sequencing increase. Here the "increment" of the selection is very critical, in this case is the GAP=GAP/2 way to choose the increment, what should be selected what kind of increment is the best, is still a mathematical problem, so far no one has found a best incremental sequence. However, a lot of research shows that when the increment sequence is dlta[k]=2t-k+1-1 (0≤k≤t≤⌊log2 (n+1) ⌋), good efficiency can be obtained. It is important to note that the last increment of the increment sequence must be equal to 1.

Sort of 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.