Insert sort-direct insert sort and Hill sort

#Sort algorithm-direct insertion sort and Hill sort
Today, we introduce the direct insertion sort and optimized sort method Shell sort in insert sort.

Direct insertion sort

Direct insertion sorting is one of the most basic sorting methods that people can think of. This sorting method divides the sequence to be sorted into two parts, one part is sorted and the other part is not sorted. At the beginning, the sorted part contains only one element, so it is naturally ordered. Each time, an element from the other part, that is, the sequence to be sorted, is compared with the already ordered sequence in sequence: the elements that are larger than it are sequentially Move one position to the right until you find the element that is smaller or smallest in the sorted part, which determines the correct position of the element to be sorted.

The dynamic demonstration diagram is as follows: the
Direct insertion sorting diagram
code is implemented as follows:

public class InsertSort {

    public static void main(String[] args) {
        Integer[] arr = {8, 6, 1, 3, 9, 7, 2, 5, 4};

        // 外层循环:每次将无序部分第一个元素插入到前面有序部分中正确位置
        for (int i = 1; i < arr.length; i++) {
            int j = i - 1;
            int tmp = arr[i];

            // 内层循环:将外层元素依次与已排序列元素比较
            while (j >= 0 && arr[j] > tmp) {
                // 保证已排序部分有序性,较大元素右移
                arr[j + 1] = arr[j];
                j--;
            }
            // 找到排序位置后插入
            arr[j+1] = tmp;
        }
        
		// test-打印
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

}

The optimal time complexity is O (n), that is, an already ordered sequence. Only each element needs to be compared once, and the average and worst case is O (n ^ 2).

Shell sort

Hill sorting is based on direct insertion sorting, which promotes the advantages of direct insertion sorting in the case of small sequences and basic ordering. It uses the idea of ​​divide and conquer to group the original sequences and then sort the elements in the group separately. Then reduce the number of groups and increase the number of elements in the group until the group is merged into one.

The dynamic demo diagram is as follows: the
Schematic diagram of shell sorting
code is implemented as follows (the demo diagram does not correspond to the code):

/**
 * @description: 希尔排序基本实现
 * @author: liyaguang
 * @create: 2018-08-31 13:41
 **/
public class ShellSort {

    public static void main(String[] args) {
        Integer[] arr = {8, 6, 1, 3, 9, 7, 2, 5, 4};

        // 确定分组个数,即确定了每个子序列,也是子序列步长
        for (int i = arr.length / 2; i > 0; i /= 2) {
            // 循环每个子序列,进行排序
            for (int j = 0; j < i; j++) {
                // j、j+i、j+i+i、……、n-i

                // 调用插入排序方法,传入子序列值
                modInsertSort(arr, j, i);
            }
        }

        // 若增量序列中不能保证最后的间距为1,则执行一次扫尾工作,此方法内不需要
        // modInsert(arr, 0, 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

    }

    /**
     * @param arr      待排数组
     * @param startPos 待排子序列起始位置
     * @param delta    步长/分组个数
     */
    public static void modInsertSort(Integer[] arr, int startPos, int delta) {
        for (int i = startPos + delta; i < arr.length - startPos; i += delta) {

            for (int j = i; j > startPos; j -= delta) {
                if (arr[j] < arr[j - delta]) {
                    // swap(arr, y, y -= i);
                    int tmp = arr[j - delta];
                    arr[j - delta] = arr[j];
                    arr[j] = tmp;
                } else {
                    break;
                }
            }
        }
    }
}

The time complexity of Hill sorting is closely related to the selected grouping strategy. In the above way, the "number of groupings divided by 2 decreases" is actually time complexity is still O (n ^ 2). Selecting the appropriate incremental sequence will Effectively improve the efficiency of Hill sorting, some can reach O (n ^ 3/2), O (n ^ 5/4), and some can even reach O (n ^ 7/6), very close to O (nlogn ).

summary

Direct insertion sorting is one of the most likely ways of sorting in our lives. For example, to sort the results of a class, we use a long enough blank paper to first record the first student in the sequence to be sorted. For the results, compare the results of the second student with the first one to find a suitable position, and then compare the third data to be ranked with the first two to achieve the final ranking effect.
The use of direct insertion sort is very intuitive and effective when the amount of data in life is small and the sequence to be sorted is basically orderly. The time complexity can be controlled at O ​​(n), while Hill sorting is based on direct insertion sort On the full play of its advantages, the use of grouping, divide and conquer the way to become smaller and smaller, and then gradually take advantage of the basic orderly results in the process of merging.

Reference:
"Data Structure and Algorithm"
Insertion sort-Wikiwand
Shellsort-wikiwand

Welcome to pay attention to my public account to learn more:
My public account

Published 159 original articles · praised 225 · 210,000 views

Guess you like

Origin blog.csdn.net/lyg673770712/article/details/82263514
Recommended