Algorithm data structure recursive insertion sort java insertion sort recursive solution insertion sort algorithm how to use recursion to write insertion sort insertion sort animation insertion sort optimization data structure (10)

1. Insertion sort (insertion-sort):

                                          It is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence. For unsorted data, scan from the back to the front in the sorted sequence, find the corresponding position and insert

    Algorithm stability:

                        For two identical numbers, after sorting, they still maintain the previous order, and the order of the two has not changed. Insertion sort is algorithmically stable

   Time complexity :

        optimal situation

                      In insertion sort, when the array to be sorted is in order, it is the optimal situation. You only need to compare the current number with the previous number. In this case, a total of N- 1 comparisons are required, and the time complexity is O(n )

        worst case scenario

                          The worst case is that the array to be sorted is in reverse order. At this time, the most number of comparisons are required. The total number of comparisons is recorded as: 1+2+3+...+N-1. Therefore, the worst case time complexity of insertion sort is O ( n_{}^{2}

     Dynamic graph :

  Recursive code:

package com.nami.algorithm.study.day06;

import java.util.Arrays;

/**
 * beyond u self and trust u self.
 *
 * @Author: lbc
 * @Date: 2023-09-05 15:36
 * @email: [email protected]
 * @Description: keep coding
 */
public class InsertionSort {

    /**
     * 插入排序:
     * 从右向左找
     *
     * @param target
     */
    public static void sort(int[] target) {
        insertion(target, 1);
    }

    /**
     * 递归 缩小结果集
     *
     * @param target
     * @param lowIndex
     */
    private static void insertion(int[] target, int lowIndex) {
        if (lowIndex == target.length) {
            return;
        }

        int t = target[lowIndex];
        // 已排序区域指针
        int i = lowIndex - 1;
        // 没有找到插入位置
        while (i >= 0 && target[i] > t) {
            target[i + 1] = target[i];
            i--;

            // 如果到达数组0时候 依旧没有找到,则退出循环
            // 抽出,合并到while内
//            if(i < 0) {
//                break;
//            }
        }
        //插入位置找到了
        // 优化减少不必要的赋值动作,
        // 需要替换的数组值,正好是大于i, i+1索引的值不需要动,这个赋值动作就不必要了
        if (i + 1 != lowIndex) {
            target[i + 1] = t;
        }
        insertion(target, lowIndex + 1);
    }

    /**
     * 两种写法,这种赋值次数更多
     * 时间复杂度相同
     * 但是 效率没有上面的高,消耗在更多的赋值操作上了
     *
     * @param target
     * @param lowIndex
     */
    private static void insertion0(int[] target, int lowIndex) {
        if (lowIndex == target.length) {
            return;
        }

        // 已排序区域指针
        int i = lowIndex - 1;
        // 没有找到插入位置
        while (i >= 0 && target[i] > target[i + 1]) {
            int temp = target[i];
            target[i] = target[i + 1];
            target[i + 1] = temp;
            i--;

        }
        insertion(target, lowIndex + 1);
    }

    public static void main(String[] args) {
        int[] test = new int[]{1, 54, 234, 675, 32432, 23, 78, 459, 354, 9, 344, 22, 46, 85, 236, 3278, 245, 83, 154, 2, 1, 34, 73, 23};
        int[] test2 = new int[]{2, 4, 7, 3, 2, 1};
//        sort(test, test.length);
        sort(test);
        System.out.println(Arrays.toString(test));
    }


}

Guess you like

Origin blog.csdn.net/qq_33919114/article/details/132707019