[Algorithm] Hill sort - an upgrade of insertion sort

Reference: "Comic Algorithm - Xiao Hui's Algorithmic Journey"

Table of contents

1. The idea of ​​Hill sorting

2. Examples

3. Hill sort code

4. Optimization of Hill sorting

5. Hill sort is an unstable sort


Q: Under what circumstances will the workload of insertion sort be relatively small?

Answer: First, when the array elements are small, the workload of insertion sort will be relatively small. Because the time complexity of insertion sorting is O(n^2), the workload is proportional to n^2. If n is relatively small, the workload of sorting will naturally be much smaller. Second, when most elements of the array are in order, the workload of insertion sorting will be relatively small. Because in this case, the elements in the array do not need to be compared and exchanged frequently.

The idea of ​​Hill sorting is similar to the meaning of the above answer, which will be explained below.

1. The idea of ​​Hill sorting

The original array is grouped step by step for rough adjustment, and the idea of ​​direct insertion sorting is carried out.

2. Examples

The original array is as follows:

 First, the elements are grouped in pairs, and the span between two elements in the same group is half of the total length of the array. In this example the group stride is 4.

 As shown in the figure above, element 5 is in a group with element 9, element 8 is in a group with element 2, element 6 is in a group with element 1, and element 3 is in a group with element 7, a total of 4 groups.

Next, we let each group of elements be sorted independently, and the sorting method can be sorted by direct insertion. Since the number of elements in each group is very small, only two, the workload of insertion sort is very small. The array after sorting each group is as follows:

We can further narrow the grouping span and repeat the above work. Reduce the span to half of the original, that is, the span is 2, and regroup the elements:

As shown in the figure, a group of elements 5, 1, 9, and 6, a group of elements 2, 3, 8, and 7, a total of two groups. Next, we continue to let each group of elements be sorted independently, and the sorting method can be directly inserted. The array after sorting each group is as follows: 

At this time, the ordering degree of the array is further improved, paving the way for the subsequent sorting. Finally, we further reduce the grouping span to 1, which is equivalent to doing direct insertion sort. After a series of rough adjustments before, the workload of direct insertion sorting has been greatly reduced, and the sorting results are as follows:

The grouping span (4, 2, 1) used in the above example is called the increment of Hill sort , and there are many options for the increment. The step-by-half incremental method we used in the example is a naive method proposed by Donald Shell when he invented Hill sort, and it is called Hill increment .

3. Hill sort code

4. Optimization of Hill sorting

Hill sorting uses the coarse tuning method of grouping to reduce the workload of direct insertion sorting, making the average time complexity of the algorithm lower than O(n^2). However, in some extreme cases, the worst time complexity of Hill sort is still O(n^2), even slower than direct insertion sort. For example:

For the above array, if we copy the previous grouping idea, whether it is incremented by 4 or incremented by 2, the elements inside each group will not be exchanged. Until we reduce the increment to 1, the array will not be adjusted according to the direct insertion sort. For such an array, Hill sort not only does not reduce the workload of direct insertion sorting, but increases the cost of grouping operations in vain. 

Each round of Hill increments is proportional, which leads to a blind spot in Hill increments. In order to avoid such extreme situations, you can choose a more effective incremental method, that is, to ensure that the increments of each round are "mutually prime" to each other. like:

  • Hibbard's incremental sequence is as follows: 1, 3, 7, 15... The worst time complexity is O(n^3/2).
  • Sedgewick's incremental sequence is as follows: 1, 5, 19, 41, 109... The worst time complexity is O(n^4/3).

5. Hill sort is an unstable sort

Hill sort is an unstable sort, and elements with the same value may be swapped. example:

 In the above array, there are two elements 5, the yellow 5 is in front, and the orange 5 is in the back. If we group by Hill increment, after the first round of rough adjustment (increment is 4), the yellow element 5 will be exchanged with the element 4, after the orange 5:

 Final sorting result:

 The same element 5 has changed its order after sorting, so it can be seen that Hill sorting is an unstable sorting.

Guess you like

Origin blog.csdn.net/weixin_45922730/article/details/129430167