Chapter 7 Sorting Algorithms (4) (Insertion Sort)

7.7 Insertion sort

7.7.1 Introduction to insertion sort method:

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

7.7.2 Insertion sort method idea:

The basic idea of ​​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 Each time during the sorting process, the first element is taken out from the unordered list, 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 becomes the new ordered list.

7.7.3 Insertion sort thought map:

insert image description here

7.7.4 Application examples of insertion sort method:

There is a group of calves, the test scores are 101, 34, 119, 1, please sort from small to large

Code:

Code for the derivation process:

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

/**
 * 插入排序
 **/
public class InsertSort {
    
    

	public static void main(String[] args) {
    
    
        int[] arr = {
    
    101, 34, 119, 1};
        System.out.println("排序前数据:");
        System.out.println(Arrays.toString(arr));

        insertSort(arr);
    }

    //插入排序
    public static void insertSort(int[] arr) {
    
    
        //使用逐步推导的方式来演示 插入排序
        //第1轮 {101, 34, 119, 1} => {34, 101, 119, 1}

        //{101, 34, 119, 1} => {34, 101, 119, 1}
        //第1轮
        //定义待插入的数
        int insertVal = arr[1];
        int insertIndex = 1 - 1;//即arr[1]的前面这个数的下标

        //给insertVal 找到插入的位置
        //说明
        //1.insertIndex >= 0,保证在给insertVal 找插入位置,不越界
        //2.insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置
        //3.就需要将arr[insertIndex] 后移
        while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
            arr[insertIndex + 1] = arr[insertIndex];
            insertIndex--;
        }
        //当退出while循环时,说明插入的位置找到,insertIndex + 1
        arr[insertIndex + 1] = insertVal;

        System.out.println("第一轮插入排序:");
        System.out.println(Arrays.toString(arr));

        //第2轮
        insertVal = arr[2];
        insertIndex = 2 - 1;

        while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
            arr[insertIndex + 1] = arr[insertIndex];
            insertIndex--;
        }
        arr[insertIndex + 1] = insertVal;

        System.out.println("第二轮插入排序:");
        System.out.println(Arrays.toString(arr));

        //第3轮
        insertVal = arr[3];
        insertIndex = 3 - 1;

        while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
            arr[insertIndex + 1] = arr[insertIndex];
            insertIndex--;
        }
        arr[insertIndex + 1] = insertVal;

        System.out.println("第三轮插入排序:");
        System.out.println(Arrays.toString(arr));
    }
}

Insertion sort code:

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

/**
 * 插入排序
 **/
public class InsertSort {
    
    

    public static void main(String[] args) {
    
    
        int[] arr = {
    
    101, 34, 119, 1};
        System.out.println("排序前数据:");
        System.out.println(Arrays.toString(arr));

        insertSort(arr);
    }

    //插入排序
    public static void insertSort(int[] arr) {
    
    
        for (int i = 1; i < arr.length; i++) {
    
    

            int insertVal = arr[i];
            int insertIndex = i - 1;

            while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            //这里我们判断是否需要赋值
            if (insertIndex + 1 != i){
    
    
                arr[insertIndex + 1] = insertVal;
            }
            System.out.println("第" + i + "轮插入排序:");
            System.out.println(Arrays.toString(arr));
        }
    }
}

Code to test insertion sort efficiency:

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

/**
 * 插入排序
 **/
public class InsertSort {
    
    

    public static void main(String[] args) {
    
    
        //测试一插入排序的速度, 给80000个数据 测试
        int arr[] = new int[80000];
        for (int i = 0, size = arr.length; i < size; i++) {
    
    
            arr[i] = (int) (Math.random() * 80000);//生成一个【0,80000)数
        }

        long startTime = System.currentTimeMillis();
        insertSort(arr);
        long endTime = System.currentTimeMillis();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start = dateFormat.format(new Date(startTime));
        String end = dateFormat.format(new Date(endTime));
        System.out.println("排序前时间:" + start);// 2023-08-20 15:11:38
        System.out.println("排序后时间:" + end);// 2023-08-20 15:11:38
    }

    //插入排序
    public static void insertSort(int[] arr) {
    
    
        for (int i = 1; i < arr.length; i++) {
    
    

            int insertVal = arr[i];
            int insertIndex = i - 1;

            while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
    
    
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            //这里我们判断是否需要赋值
            if (insertIndex + 1 != i){
    
    
                arr[insertIndex + 1] = insertVal;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_45828554/article/details/132392138