zoukankan      html  css  js  c++  java
  • 插入排序

    插入排序

    插入排序法思想

    插入排序(Insertion Sorting)的基本思想是:把n个待排序的元素看成为一个有序表和一个无序表,开始时有序表中只包含一个元素,无序表中包含有n-1个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码依次与有序表元素的排序码进行比较,将它插入到有序表中的适当位置,使之成为新的有序表。

    /**
     * 插入排序
     */
    public class InsertSort {
    
        public static void main(String[] args) {
           insetSort(creat());
        }
    
        public static int[] creat() {
    
            int[] arr = new int[1000000];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = new Random().nextInt(500000);
            }
            return arr;
        }
    
        public static void insetSort( int[] arr) {
            long star=System.currentTimeMillis();
            for (int i = 1; i < arr.length; i++) {
                int temp = arr[i];
                int index = i-1;
                while (index>=0&&arr[index]>temp){
                    arr[index+1]=arr[index];
                    index--;
                }
                arr[index+1]=temp;
            }
            long end=System.currentTimeMillis();
            System.out.println(end-star);
        }
    }
    
    
  • 相关阅读:
    box-sizing: border-box的作用
    什么时候用created,什么时候用mounted
    圣杯布局
    关闭浏览器 清除session
    js this
    js 深拷贝
    经常犯的一些小错误
    语法:c++对关于空指针0/NULL/nullptr三者的演变
    常用的函数
    C++一些小技巧
  • 原文地址:https://www.cnblogs.com/huangshen/p/13324317.html
Copyright © 2011-2022 走看看