Starting from sorting (2) Hill sorting

Source: Internet
Author: User

Hill sorting is an improved version of direct insertion sorting, also known as incremental reduction sorting, which is essentially a sort of group insertion. The basic idea is to first take the first incremental step, and take the elements with different subscripts in the sequence as a group, such as array [0], array [0 + step], array [0 + step * 2]... as a group, array [1], array [1 + step], array [1 + step * 2]... as a group, then directly insert and sort these groups respectively, then reduce the increment, repeat the above operation, and finally step = 1, then the sequence is basically ordered, directly insert and sort the array again. In this case, the complexity of directly insert sorting is O (n), and the final result is an ordered series. The complexity of hill sorting is complex, mainly related to step size selection, which is probably O (n logn ^ 2). It is generally considered to be between O (n ^ 2) and O (n logn). It is recommended that the step size be complex. Generally, half of the sequence is obtained for the first time, and then halved each time until the step size is 1. For why the sort by hill is superior to the sort by direct insertion, let's look at the Wikipedia: "The sort by hill improves the performance of the sort by dividing all the compared elements into several regions. This allows an element to take a big step toward the final position at a time. Then the algorithm sorts the data with smaller and smaller step sizes. the last step of the algorithm is normal insertion sorting, but in this step, the data to be sorted is almost sorted (insertion sorting is faster at this time )." "Perhaps the most important thing about Hill sorting is that after small step sizes are used, the large step sizes used in the past are still ordered. For example, if a sequence is sorted by Step 5 and then by step 3, the sequence is not only ordered by step 3, but also by Step 5. If this is not the case, the algorithm will disrupt the previous order in the iteration process, and the sorting will not be completed in such a short time ." Complexity: The worst time complexity: varies depending on the serial step. The best known O (n logn ^ 2) optimal time complexity: O (n) Average time complexity: varies depending on the serial step. Worst space complexity: O (n) Stability: unstable implementation:

Void shellSort (int data [], int n) {int I, j, step; for (step = n/2; step> 0; step/= 2) // step, take half each time {for (I = step; I <n; ++ I) // starting from step, each element is inserted into its own Group {int key = data [I]; for (j = I-step; j> = 0 & data [j]> key; j-= step) // locate the corresponding position of the element in the group. data [j + step] = data [j];

 

Data [j + step] = key; // Why j + step? Remember to directly insert and sort the 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.