Java programming: sorting algorithm-insertion sort

basic introduction

Insertion sorting belongs to the internal sorting method, which is to find the appropriate position of the element to be sorted by inserting it to achieve the purpose of sorting.

Insert sorting idea

The basic idea of ​​insertion sorting (Insertion Sorting) is: treat n elements to be sorted as an ordered list and an unordered list. At the beginning, the ordered list contains only one element, and the unordered list contains n-1 Elements, each time the first element is taken from the unordered list during the sorting process, its sort code is compared with the sort code of the ordered list elements in turn, and it is inserted into the appropriate position in the ordered list to make it Become a new ordered list.

Insert sort idea diagram

Insert picture description here

Insert sort application example

There is a group of calves, the test scores are 101, 34, 119, 1 Please sort from small to large
Insert picture description here

Code

package sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class InsertSort {
    
    
    public static void main(String[] args) {
    
    
        //int[] arr = {101, 34, 119, 1, -1, 89, 27};
        //insertSort(arr);
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
    
    
            arr[i] = (int) (Math.random() * 80000);// 生成一个0-80000的数据
        }
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1Str = simpleDateFormat.format(date1);
        System.out.println("排序前的时间为:" + date1Str);
        insertSort(arr);
        Date date2 = new Date();
        String date2Str = simpleDateFormat.format(date2);
        System.out.println("排序后的时间为:" + date2Str);
        //System.out.println(Arrays.toString(arr));
    }

    public static void insertSort(int[] arr) {
    
    
        // 使用for循环
        int insertVal = 0;
        int insertIndex = 0;
        for (int i = 1; i < arr.length; i++) {
    
    
            insertVal = arr[i];
            insertIndex = i - 1;    // 及arr[1] 前面数字的下标
            // 给insertVal 找到插入的位置
            while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
                // 说明
                // 1. insertIndex >= 0 保证给insertVal找插入位置不越界
                // 2. insertVal < arr[insertIndex] 说明待插入数字还没有找到适当位置
                // 3. 就需要将insertIndex后移
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            // 当退出while循环时,说明插入位置找到,insertIndex + 1
            // 判断是否需要赋值
            if (insertIndex + 1 != i) {
    
    
                arr[insertIndex + 1] = insertVal;
            }

        }

    }

    // 插入排序
    public static void insertSort1(int[] arr) {
    
    
        // 使用逐步推导的方法讲解
        // 第1轮 {101, 34, 119, 1} →→→ {34, 101, 119, 1}

        // 定义待插入的数字
        int insertVal = arr[1];
        int insertIndex = 1 - 1;    // 及arr[1] 前面数字的下标
        // 给insertVal 找到插入的位置
        while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
            // 说明
            // 1. insertIndex >= 0 保证给insertVal找插入位置不越界
            // 2. insertVal < arr[insertIndex] 说明待插入数字还没有找到适当位置
            // 3. 就需要将insertIndex后移
            arr[insertIndex + 1] = arr[insertIndex];
            insertIndex--;
        }
        // 当退出while循环时,说明插入位置找到,insertIndex + 1
        arr[insertIndex + 1] = insertVal;
        System.out.println("第1轮插入后:");
        System.out.println(Arrays.toString(arr));

        // 第2轮 {101, 34, 119, 1} →→→ {34, 101, 119, 1}

        // 定义待插入的数字
        insertVal = arr[2];
        insertIndex = 2 - 1;    // 及arr[1] 前面数字的下标
        // 给insertVal 找到插入的位置
        while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
            // 说明
            // 1. insertIndex >= 0 保证给insertVal找插入位置不越界
            // 2. insertVal < arr[insertIndex] 说明待插入数字还没有找到适当位置
            // 3. 就需要将insertIndex后移
            arr[insertIndex + 1] = arr[insertIndex];
            insertIndex--;
        }
        // 当退出while循环时,说明插入位置找到,insertIndex + 1
        arr[insertIndex + 1] = insertVal;
        System.out.println("第2轮插入后:");
        System.out.println(Arrays.toString(arr));

        // 第3轮 {101, 34, 119, 1} →→→ {1, 34, 101, 119}

        // 定义待插入的数字
        insertVal = arr[3];
        insertIndex = 3 - 1;    // 及arr[1] 前面数字的下标
        // 给insertVal 找到插入的位置
        while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
            // 说明
            // 1. insertIndex >= 0 保证给insertVal找插入位置不越界
            // 2. insertVal < arr[insertIndex] 说明待插入数字还没有找到适当位置
            // 3. 就需要将insertIndex后移
            arr[insertIndex + 1] = arr[insertIndex];
            insertIndex--;
        }
        // 当退出while循环时,说明插入位置找到,insertIndex + 1
        arr[insertIndex + 1] = insertVal;
        System.out.println("第3轮插入后:");
        System.out.println(Arrays.toString(arr));
    }
}


in conclusion

80,000 data takes 1-2 seconds, which is faster than bubbling.

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108780661