• 直接插入排序3


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

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

    先拿一张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 进行比较,确定要插入的合适位置。这就需要一个内部循环,我们一般是从后往前比较,即从下

    主要代码
    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

     

     package notes.javase.algorithm.sort;
     
     import java.util.Random;
      
     public class InsertSort {
      
        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);
             }
         }
      
         // 打印序列
         public void printPart(int[] list, int begin, int end) {
             for (int i = 0; i < begin; i++) {
                 System.out.print("	");
             }
             for (int i = begin; i <= end; i++) {
                 System.out.print(list[i] + "	");
             }
             System.out.println();
         }
      
         public static void main(String[] args) {
             // 初始化一个随机序列
             final int MAX_SIZE = 10;
             int[] array = new int[MAX_SIZE];
            Random random = new Random();
             for (int i = 0; i < MAX_SIZE; i++) {
                 array[i] = random.nextInt(MAX_SIZE);
             }
     
             // 调用冒泡排序方法
             InsertSort insert = new InsertSort();
            System.out.print("排序前:	");
             insert.printPart(array, 0, array.length - 1);
             insert.insertSort(array);
             System.out.print("排序后:	");
             insert.printPart(array, 0, array.length - 1

    运行结果

    排序前:    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   
    运行结果
  • 相关阅读:
    Ubuntu 14.04 LTS Server 无法挂载光盘 启动initramfs等问题
    Linux的交叉编译 及configure配置
    大话设计模式读书笔记(五) 代理模式
    大话设计模式读书笔记(三) 单一职责原则和开放-封闭原则和依赖倒转原则
    大话设计模式读书笔记(二) 策略模式
    Java NIO(一) 初步理解NIO
    大话设计模式读书笔记(一) 简单工厂模式
    多线程设计模式(一) Single Threaded Execution
    多线程详细解析(二) 线程的共享互斥与线程的协调
    多线程详细解析(一) 创建线程
  • 原文地址:https://www.cnblogs.com/2228212230qq/p/8119518.html
Copyright © 2020-2023  润新知