The Hill sort of dead-knock algorithm

Source: Internet
Author: User

To learn more about the algorithm series, please refer to the article: summary of dead-knock algorithm

Today to tell the Hill sort, hill sort, actually can be understood as the insertion algorithm sort of an upgrade version, do not know the insertion sort of small partners can first look at this article: dead-knock algorithm insertion Sort

We know that when the insertion sort is sorted, if there is a very small amount of data at the end of the array when the data is large, then we need to move all the elements in front of the data to the appropriate element. For example:

We want to sort the array for [1,2,3,4,5,6,7, ... Omitted here 1 million ..., 0]. Detailed everyone certainly do not like this 0 move forward 1 million this bar.

The appearance of hill sort is actually to solve this problem, hill sort, using the divide and conquer algorithm, first the whole large array according to a certain increment is divided into several groups, first of these groups to make a adjustment to ensure that most of the small data will be adjusted to the front. In the end, the insertion sort is done again, which greatly accelerates the efficiency.

To an example, we want to sort the array for [3, 1, 0, 2, 8, 4, 2,6,9,1,3,-2,8], first look at a picture

    1. The above-mentioned increment is the basis for our grouping. Here we get the initial value is one of the 2 points of the array (this value does not have a standard definition, just guarantee greater than 1 and less than the length of the array), and the red line is pointing to us according to the increment of the group, we separately for each group to sort.
    2. Can be seen in the results of the increment of 3, the first group of 3,2,8 changed to 2,3,8, the second group of the third group unchanged, the fourth group became 1, 2, fifth Group became 3,8, sixth group became -2,4.
    3. The next increment is halved, and our array is divided into 3 groups, sorted separately.
    4. Now that the increment value has been halved again and has become 1, we can observe the array to find that it is not possible to have the smallest data behind the array, and now the efficiency of inserting and sorting the arrays is very high.

Don't know now you understand Hill sort? Let's take a look at the code.

void shellsort  (int list[], int length) {int gap,i,j,temp; for (Gap = Length/2; gap > 0; Gap/= 2) {for (i = gap; i < length; i++) {0 && list[j]>list[j+gap"; J-= gap) {temp = list[j]; list[j] = list[j+gap]; list[j+gap] = temp;} } }} 

The hill sort is done. Here warmly remind everyone, learning algorithms, we do not have to rigidly adhere to the implementation of the code, it is meaningless. My advice is to understand the steps in depth, and when you understand the steps, the code is as good as you can play.

The Hill sort of dead-knock algorithm

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.