• 排序三 直接插入排序


    目录

    要点

    直接插入排序是一种最简单的插入排序

    插入排序:每一趟将一个待排序的记录,按照其关键字的大小插入到有序队列的合适位置里,知道全部插入完成。 

    在讲解直接插入排序之前,先让我们脑补一下我们打牌的过程。

    先拿一张5在手里,

    再摸到一张4,比5小,插到5前面,

    摸到一张6,嗯,比5大,插到5后面,

    摸到一张8,比6大,插到6后面,

    。。。

    最后一看,我靠,凑到全是同花顺,这下牛逼大了。 

    以上的过程,其实就是典型的直接插入排序,每次将一个新数据插入到有序队列中的合适位置里

    很简单吧,接下来,我们要将这个算法转化为编程语言。

    假设有一组无序序列 R0, R1, ... , RN-1。

    (1) 我们先将这个序列中下标为 0 的元素视为元素个数为 1 的有序序列。

    (2) 然后,我们要依次把 R1, R2, ... , RN-1 插入到这个有序序列中。所以,我们需要一个外部循环,从下标 1 扫描到 N-1 。

    (3) 接下来描述插入过程。假设这是要将 Ri 插入到前面有序的序列中。由前面所述,我们可知,插入Ri时,前 i-1 个数肯定已经是有序了。

    所以我们需要将Ri 和R0 ~ Ri-1 进行比较,确定要插入的合适位置。这就需要一个内部循环,我们一般是从后往前比较,即从下标 i-1 开始向 0 进行扫描。 

    核心代码

     
    public void insertSort(int[] list) {
        // 打印第一个元素
        System.out.format("i = %d: ", 0);
        printPart(list, 0, 0);
     
        // 第1个数肯定是有序的,从第2个数开始遍历,依次插入有序序列
        for (int i = 1; i < list.length; i++) {
            int j = 0;
            int temp = list[i]; // 取出第i个数,和前i-1个数比较后,插入合适位置
     
            // 因为前i-1个数都是从小到大的有序序列,所以只要当前比较的数(list[j])比temp大,就把这个数后移一位
            for (j = i - 1; j >= 0 && temp < list[j]; j--) {
                list[j + 1] = list[j];
            }
            list[j + 1] = temp;
     
            System.out.format("i = %d: ", i);
            printPart(list, 0, i);
        }
    }
     

    算法分析

    直接插入排序的算法性能

    排序类别

    排序方法

    时间复杂度

    空间复杂度

    稳定性

    复杂性

    平均情况

    最坏情况

    最好情况

    插入排序

    直接插入排序

    O(N2)

    O(N2)

    O(N)

    O(1)

    稳定

    简单

    时间复杂度 

    当数据正序时,执行效率最好,每次插入都不用移动前面的元素,时间复杂度为O(N)。 

    当数据反序时,执行效率最差,每次插入都要前面的元素后移,时间复杂度为O(N2)

    所以,数据越接近正序,直接插入排序的算法性能越好。 

    空间复杂度

    由直接插入排序算法可知,我们在排序过程中,需要一个临时变量存储要插入的值,所以空间复杂度为 1 。

    算法稳定性

    直接插入排序的过程中,不需要改变相等数值元素的位置,所以它是稳定的算法。 

    完整参考代码

    JAVA版本

    代码实现

    1 package notes.javase.algorithm.sort;
     2  
     3 import java.util.Random;
     4  
     5 public class InsertSort {
     6  
     7     public void insertSort(int[] list) {
     8         // 打印第一个元素
     9         System.out.format("i = %d: ", 0);
    10         printPart(list, 0, 0);
    11  
    12         // 第1个数肯定是有序的,从第2个数开始遍历,依次插入有序序列
    13         for (int i = 1; i < list.length; i++) {
    14             int j = 0;
    15             int temp = list[i]; // 取出第i个数,和前i-1个数比较后,插入合适位置
    16  
    17             // 因为前i-1个数都是从小到大的有序序列,所以只要当前比较的数(list[j])比temp大,就把这个数后移一位
    18             for (j = i - 1; j >= 0 && temp < list[j]; j--) {
    19                 list[j + 1] = list[j];
    20             }
    21             list[j + 1] = temp;
    22  
    23             System.out.format("i = %d: ", i);
    24             printPart(list, 0, i);
    25         }
    26     }
    27  
    28     // 打印序列
    29     public void printPart(int[] list, int begin, int end) {
    30         for (int i = 0; i < begin; i++) {
    31             System.out.print(" ");
    32         }
    33         for (int i = begin; i <= end; i++) {
    34             System.out.print(list[i] + " ");
    35         }
    36         System.out.println();
    37     }
    38  
    39     public static void main(String[] args) {
    40         // 初始化一个随机序列
    41         final int MAX_SIZE = 10;
    42         int[] array = new int[MAX_SIZE];
    43         Random random = new Random();
    44         for (int i = 0; i < MAX_SIZE; i++) {
    45             array[i] = random.nextInt(MAX_SIZE);
    46         }
    47  
    48         // 调用冒泡排序方法
    49         InsertSort insert = new InsertSort();
    50         System.out.print("排序前: ");
    51         insert.printPart(array, 0, array.length - 1);
    52         insert.insertSort(array);
    53         System.out.print("排序后: ");
    54         insert.printPart(array, 0, array.length - 1);
    55     }
    56 }
    复制代码


    运行结果 

     
    排序前:    6    3    3    5    6    3    1    0    6    4    
    i = 0:    6    
    i = 1:    3    6    
    i = 2:    3    3    6    
    i = 3:    3    3    5    6    
    i = 4:    3    3    5    6    6    
    i = 5:    3    3    3    5    6    6    
    i = 6:    1    3    3    3    5    6    6    
    i = 7:    0    1    3    3    3    5    6    6    
    i = 8:    0    1    3    3    3    5    6    6    6    
    i = 9:    0    1    3    3    3    4    5    6    6    6    
    排序后:    0    1    3    3    3    4    5    6    6    6   
     

    参考资料

    《数据结构习题与解析》(B级第3版)

    http://www.cnblogs.com/huangxincheng/archive/2011/11/20/2255695.html 

    相关阅读

    欢迎阅读 程序员的内功——算法 系列

    示例源码:https://github.com/dunwu/algorithm-notes

  • 相关阅读:
    [LeetCode] 582. Kill Process
    [LeetCode] 686. Repeated String Match
    [LeetCode] 341. Flatten Nested List Iterator
    [LeetCode] 404. Sum of Left Leaves
    [LeetCode] 366. Find Leaves of Binary Tree
    [LeetCode] 1485. Clone Binary Tree With Random Pointer
    [LeetCode] 459. Repeated Substring Pattern
    [LeetCode] 565. Array Nesting
    [LeetCode] 679. 24 Game
    [LeetCode] 364. Nested List Weight Sum II
  • 原文地址:https://www.cnblogs.com/aimei/p/12199880.html
Copyright © 2020-2023  润新知