Sorting algorithm --- insertion sort (java version)

Direct insertion sort

principle

The principle of Insertion Sort is to divide the data in the array into two intervals, a sorted interval and an unsorted interval. The initial sorted interval has only one element, which is the first element of the array. The core idea of ​​the insertion algorithm is to take the elements in the unsorted interval, find a suitable insertion position in the sorted interval and insert it, and ensure that the sorted interval data is always in order. Repeat this process until the elements in the unsorted interval are empty and the algorithm ends.

The algorithm process is shown in the figure below
Insert picture description here

The algorithm is described as follows:

  • Starting from the first element, the element can be considered to have been sorted;
  • Take out the next element and scan from back to front in the sorted sequence of elements;
  • If the element (sorted) is larger than the new element, move the element to the next position;
  • Repeat step 3 until you find the position where the sorted element is less than or equal to the new element;
  • After inserting the new element into this position;
  • Repeat steps 2~5.

Code

public class InsertSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    101, 34, 98, 1};
        int insertVal = 0;
        int insertIndex = 0;
        for (int i = 1; i < arr.length; i++) {
    
    
            //取出未排序的下一个元素,及当前参与比较的元素
            insertVal = arr[i];
            //在已经排序的元素序列中从后向前扫描,定义前置索引
            insertIndex = i - 1;

            //insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置
            //需要将 arr[insertIndex] 后移 即 插入的元素一直和它前面的值进行比较,直到找到正确的位置,之后将该位置的值进行插入
            while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            //正确的位置不交换元素
            if (insertIndex + 1 !=i) {
    
    
                arr[insertIndex + 1] = insertVal;
            }
            System.out.println(Arrays.toString(arr));
        }
    }
}

1: What is the time complexity of insertion sort?

If the data to be sorted is already in order, we don't need to move any data. If we search for the insertion position in the ordered data group from end to beginning, we only need to compare one data at a time to determine the insertion position. So in this case, the best time complexity is O(n). Note that here is to traverse the ordered data from end to beginning. If the array is in reverse order, each insertion is equivalent to inserting new data at the first position of the array, so a large amount of data needs to be moved, so the worst-case time complexity is O(n^2). The average time complexity of inserting a piece of data in an array is O(n^2). Therefore, for insertion sort, each insertion operation is equivalent to inserting a piece of data in the array, and performing n insertion operations in a loop, so The average time complexity is O(n^2).

2: What is the space complexity of insertion sort?

It is obvious from the implementation process that the insertion sort algorithm does not require additional storage space to run, so the space complexity is O(1), that is to say, this is an in-place sorting algorithm .

3: Is insertion sort a stable sorting algorithm?

In insertion sort, for elements with the same value, we can choose to insert the elements that appear later after the elements that appear before, so that the original order can be kept unchanged, so insertion sort is a stable sorting algorithm.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113178437