Sort data structures and algorithms six: Hill sort

Source: Internet
Author: User

With the introduction of the previous five sorting methods, we learned about recursive thinking and the merging and quick ordering of divide and conquer, and of course the more straightforward data value transfer bubbling, selection, and insertion sort. It can be said that each sort of different ways, are suitable for use in a variety of environments, but we sometimes do not think of a problem, that is, we are learning to insert the sort of the theme of the idea is to take each data out, and then the front of the data from the back of the comparison, then, Are we going to do N-1 cycles, or do we want to do a squared comparison of about N, we know that the exchange and comparison of values is very time consuming, and that is not against the development of our algorithm. There is no better way to solve, in the vast amount of data, we can modify the insertion to achieve an ideal result. Well, we're going to introduce the hill sort that came into being, if you're not very familiar with the insertion sort, then I suggest going through the insertion sort and then looking at the hill sort, in my cognition, the hill sort or can be described as an upgrade of the insertion sort, the core idea he used is to insert sort. We mentioned the idea of divide and conquer, in the vast amount of data, we can extract a part of it as a group, then have n groups, our team of these groups to sort, then the overall array can we think of as a discontinuous array, And then we're going to be able to avoid repeated comparisons, or data exchanges, when we're doing the whole insertion. So, if you see this, you should be able to understand the hill sort, in fact, is a whole unit, preprocessing, and then the overall processing, this and merge sort is not a bit coincide, divide and conquer. Words do not understand, then we directly:

      

We can see the thought, we choose the size of the unit is related to our stride length, that is, we have more than one step, then we have a few preprocessing of this array, in such a case, always our stride will be 1, So that means we do the necessary processing at the end of the insertion sort, and we will use the fewest data exchanges and comparisons when sorting. It can be said that the hill sort is better at processing a large amount of data than in the insert sort, which is also to reduce the amount of data and keep the data intermittently organized, reducing the number of comparisons and exchanges of data. For example, we are not lucky, the last data is the smallest value, if we continue to use the insertion sort does not mean that we have to make a comparison of N-1 times, the time consumed for us is a little unacceptable, if we follow the comparison of a certain step size, so that intermittent order, is not going to reduce the number of comparison steps, the code is served:

1  Public Static voidShellsort (intarr[]) {2       intn =arr.length;3       //Select Step Size4        for(intGap = N/2;gap >= 1;gap/= 2){5            //Select group6             for(inti = 0;i < gap;i++){7                 //the key value that needs to be inserted8                  for(intj = i + gap;j < N;j + =Gap) {9                       //compares the value to the previous value, whether an exchange is requiredTen                       if(Arr[j] < arr[j-Gap]) { One                             intKey =Arr[j]; A                             intK = J-Gap; -                              while(k >= 0 && Key <Arr[k]) { -                                   //Perform value Exchange theArr[k + gap] =Arr[k]; -                             } -                             //the selected key value is returned to the position -Arr[k + gap] =key; +                       }  -                 } +            }      A       }                   at}

From the above code can be more intuitive to feel, in fact, Hill sort is the first to divide a whole into countless unit fragments, according to the step, and then the order of these units to achieve intermittent order of effect, finally in the overall insertion sort, we can see, in the first for loop, We're actually trying to get a step size that is shrinking, which means that the overall grouping is shrinking until it's over. Then we select a different group in the second loop, in fact, this is my understanding, it can be said that the different starting value and the step size to obtain a certain amount of data as a group of data. In the third loop we have a normal insertion of the data for this group, and the hill sort is a sort of insertion with a certain step, so we can understand that!

Issues to be aware of:

One: About Hill Sort Step selection question:

To tell the truth, I really do not understand the problem, it is not very convenient to make the corresponding explanation, but in my view of the algorithm fourth edition books and data structures and algorithms for this issue is to choose N/3 as the step, but most people choose N/2 as the step, In fact, we found a perfect step can help us a lot, but unfortunately, I can not give this explanation, I am interested to see other professional explanations. The choice of step size is closely related to the efficiency of hill sequencing, which we know is sufficient.

Two: code optimization on the hill sort:

If we can pay attention to, see a lot of people have a strong interest in the hill sort, and try to constantly optimize, but, I want to say, so that many places in the hill has been deformed, leaving the main idea, In fact, we are learning an algorithm at the time of their own level is not enough to grasp the most basic interpretation of the code can be translated, my upper code may seem more complex, but it should be the easiest to understand a way to understand. Seizing two key questions, the control of step change, the method of inserting sort for the unit fragment with the step-base, this is the most basic hill sort, if I raise the level later, then we will discuss the step problem of hill problem.

  

Sort data structures and algorithms six: Hill sort

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.