Hill sorting [Java algorithm]

1. Concept

Hill sort is also an insertion sort. It is a more efficient version of simple insertion sort and is also called shrinking incremental sort. Hill sorting uses a jump grouping strategy in the array, dividing the array elements into several groups through a certain increment, and then groups them for insertion sorting, then gradually reduces the increment, and continues to perform insertion sorting operations by group until the increment is 1.

I recommend a six-minute video from Station B. The PPT animation is very well done and clear.

Insert image description here

2. Ideas

① Hill sorting uses a jump grouping method. What is a jump group? That is to say, members of the same group are actually some distance apart from each other in the original sequence. They are forced to be drawn out to form a team, and insertion sort is used internally to compare sizes. The distance between them is equal. We call this distance an increment. How to determine the increment? Generally speaking, the initial increment value is half of the sequence length. Next, we calculate the grouping based on the increment value. As shown in the figure below, the increment is 5, so index 0 is a group with index 5, index 1 is a group with index 6..., so By analogy, the sequences are divided into five groups;

Insert image description here
② After being divided into groups, insertion sorting is performed within the group members, but the insertion sort step here is actually not 1. We know that the original insertion sort is to compare the sizes and insert step by step from right to left, but Hill The sorting is grouped by jump. Although you are assigned to the same team, don't forget that your own indexes are not connected. The index is still the index of the original position. Therefore, when inserting the sorting here, each step The length should be the size of the increment. Except for the different step length, other ideas remain unchanged;

③ After the first round of sorting was completed, it was found that there was a general trend in the order of the sequences as a whole, with the small ones basically on the left and the big ones on the right. However, this is the first step and it is not yet sorted;

④ Start the next round of sorting. The increment value at the beginning of each round should be half of the increment value in the previous round. The principle remains the same, external grouping and internal insertion sort. When will it no longer be grouped or sorted? The incremental value keeps halving, and one day it will be reduced to 1. When it reaches 1, the entire sequence will undergo the most basic insertion sort. Yes, this is the last step, so the termination condition is that the incremental value starts to be less than 1. This The sequence of time is already completely in order.

3. Code implementation

import java.util.Arrays;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    2, 9, 3, 11, 7, 8, 4, 1, 6};
        int[] newArr = sort(arr);
        System.out.println(Arrays.toString(newArr));
    }
    public static int[] sort(int[] arr) {
    
    
        //控制增量值,初始值为序列长度的一半,每次减半,步长为0时停止
        for (int step = arr.length / 2; step > 0; step /= 2) {
    
    
            //控制待插入元素的位置,初始值为增量值
            for (int i = step; i < arr.length; i++) {
    
    
                //待插入元素
                int insertVal = arr[i];
                //待比较元素初始位置
                int index = i - step;
                //控制待比较元素的位置,初始值为待插入元素的位置减去增量值,即index
                while (index >= 0 && insertVal < arr[index]) {
    
    
                    //当前待比较元素向后移一位,这里的一位就是step长度
                    arr[index + step] = arr[index];
                    //指针向左挪动一位,继续跟下一个元素作比较
                    index -= step;
                }
                //退出循环后后,将待插入元素插入到index的下一位
                arr[index + step] = insertVal;
            }
        }
        return arr;
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/m0_52861684/article/details/132299472