Java basic insertion sort (direct insertion sort, Hill sort)

1. Basic concepts

1. Concept:

  • Sorting is the operation of arranging a string of records in increasing or decreasing order according to the size of one or some keywords. In the usual context, if you mention sorting, it usually means sorting ascending (not descending)

2. Stability

  • If two equal data are sorted, the sorting algorithm can ensure that their relative position does not change, then we call the algorithm a stable sorting algorithm
    .
  • Let's use a simple diagram to explain to everyone: the data given in the figure has two 3s. If after sorting, the black 3 is still after the red 3, then this sorting is stable, otherwise it is unstable.
    Insert picture description here

3.Java common sort

Insert picture description here

2. Implementation of specific sorting code

1. Direct insertion sort

  • Stability (stable): Stable sorting will not exchange data jumps during the sorting process
    . 1. A stable sort can become an unstable sort;
    2. An unstable sort by itself cannot become stable;

  • Space complexity: O(1);

  • time complexity:

    • Best case: when the O(n) array is ordered;
    • Worst case: the worst case O(n2) when the array is out of order;
  • Idea : Take ascending order as an example
    1. First use i to traverse the array from the beginning;
    2. Then take a space tmp outside and put arr[i] in each time;
    3. Then define a variable j, the initial value of j i-1;
    4. Compare the size of arr[j] and tmp, if it is greater than tmp, put arr[j] at the j+1 position;
    5. It will be arranged until i traverses the array.

public static void insertsort(int[] arr){
    
    
        for(int i = 1;i < arr.length;i++) {
    
    
            int tmp = arr[i];
            int j = i-1;
            for (;j >= 0;j--){
    
    
                if (arr[j] > tmp) {
    
    
                    arr[j+1] = arr[j];
                }else{
    
    
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

2. Hill sort

  • Stability (unstable)

​ 1. A stable sort can become an unstable sort;

​ 2. An inherently unstable one cannot become stable;

  • Space complexity: O(n^1.3)-O(n^1.5);

  • time complexity:

    • Best case: when the O(n) array is ordered;
    • Worst case: the worst case O(n^2) when the array is out of order;
  • Idea : Ascending order as an example
    1. Hill sorting is actually an optimization of direct insertion sorting;
    2. Grouping a group of data, such as the following figure is divided into 5 groups, gap == 5, starting from position 0, add gap , So as to determine the elements in a group, the same color below is a group;
    3. Then each group is sorted by direct insertion sorting;
    4. This time the row is finished, the next time it is divided into 3 groups, in Call direct insertion sort, the next time you can divide it into 2 groups or directly into one group;
    5. Keep grouping until it is divided into one group and sort the order, then the sorting will be done;
    6. Then everyone definitely wants to know how to choose gap Have you found that the number of groups is prime, so the number of groups is selected according to the size of the data. For example, the following group of data has 15 elements, which are divided into 5, 3, and 1 groups.
    Insert picture description here

//希尔排序(优化直接插入排序)
    public static void shellsort(int[] arr){
    
    
        //1.首先获取组数gqp
        int[] drr = {
    
    5,3,1};//定义一个增量数组
        for (int i = 0;i < drr.length;i++){
    
    
            shell(arr,drr[i]);
        }
    }
    //直接插入排序
    public static void shell(int[] arr,int gap) {
    
    
        for(int i = gap;i < arr.length;i++) {
    
    
            int tmp = arr[i];
            int j = i-gap;
            for (;j >= 0;j -= gap){
    
    
                if (arr[j] > tmp) {
    
    
                    arr[j+gap] = arr[j];
                }else{
    
    
                    break;
                }
            }
            arr[j+gap] = tmp;
        }
    }

3. Result test

public class TestSort {
    
    
//直接插入排序
public static void insertsort(int[] arr){
    
    
        for(int i = 1;i < arr.length;i++) {
    
    
            int tmp = arr[i];
            int j = i-1;
            for (;j >= 0;j--){
    
    
                if (arr[j] > tmp) {
    
    
                    arr[j+1] = arr[j];
                }else{
    
    
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }
    //希尔排序(优化直接插入排序)将数组进行分组
    public static void shellsort(int[] arr){
    
    
        //1.首先获取组数gqp
        int[] drr = {
    
    5,3,1};//定义一个增量数组
        for (int i = 0;i < drr.length;i++){
    
    
            shell(arr,drr[i]);
        }
    }
    //直接插入排序
    public static void shell(int[] arr,int gap) {
    
    
        for(int i = gap;i < arr.length;i++) {
    
    
            int tmp = arr[i];
            int j = i-gap;
            for (;j >= 0;j -= gap){
    
    
                if (arr[j] > tmp) {
    
    
                    arr[j+gap] = arr[j];
                }else{
    
    
                    break;
                }
            }
            arr[j+gap] = tmp;
        }
    }
//测试main方法
public static void main(String[] args) {
    
    
        //直接插入排序
        int[] arr = {
    
    2,3,5,1,6,4};
        insertsort(arr);
        System.out.print("直接插入排序结果: ");
        for (int i = 0;i < arr.length;i++){
    
    
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        //希尔排序
        int[] arr1 = {
    
    7,4,9,34,0,8,5,22,55,6,12,33,56,89,77};
        shellsort(arr1);
        System.out.print("希尔排序结果: ");
        for (int i = 0;i < arr1.length;i++){
    
    
            System.out.print(arr1[i]+" ");
        }
        System.out.println();
}
}

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/109646040