最近从网易公开课在看麻省理工学院的公开课《算法导论》,感觉还不错,接下来几篇文章所示学习日记了,不准备对算法细节做过多描述,感兴趣的可以自己去看。

文章分几篇讲经典排序算法,直接上代码,根据结果对算法性能有个直观了解。本篇先说插入排序(insertion sort)。

(一)算法实现

 protected void sort(int[] toSort) {
if (toSort.length <= 1) {
return;
}
for (int i = 1; i < toSort.length; i++) {
if (toSort[i] < toSort[i - 1]) {
int j = i;
int temp = toSort[i];
while (j > 0 && temp < toSort[j - 1]) {
toSort[j] = toSort[j - 1];
j--;
}
toSort[j] = temp;
}
}
}

Insertion sort

1)插入排序属于原地排序,节省空间

2)插入排序的时间复杂度是O(n2)

3)插入排序属于比较排序

4)插入排序属于稳定排序算法

(二)算法性能

**************************************************
Number to Sort is:2500
Array to sort is:{665184,192100,475135,171530,869545,506246,640618,543738,91353,493005...}
Cost time of 【InsertionSort】 is(milliseconds):3
Sort result of 【InsertionSort】:{856,985,2432,3792,3910,3915,4423,4516,4653,4780...}
**************************************************
Number to Sort is:25000
Array to sort is:{99880,631403,265087,597224,876665,955084,996547,879081,197806,926881...}
Cost time of 【InsertionSort】 is(milliseconds):267
Sort result of 【InsertionSort】:{14,14,17,83,97,152,179,199,240,299...}
**************************************************
Number to Sort is:250000
Array to sort is:{777293,731773,508229,920721,338608,707195,940,445210,19071,768830...}
Cost time of 【InsertionSort】 is(milliseconds):21,523
Sort result of 【InsertionSort】:{2,7,7,19,19,21,24,29,30,39...}

相关代码:

 package com.cnblogs.riyueshiwang.sort;

 import java.util.Arrays;

 public class InsertionSort extends abstractSort {
@Override
protected void sort(int[] toSort) {
if (toSort.length <= 1) {
return;
}
for (int i = 1; i < toSort.length; i++) {
if (toSort[i] < toSort[i - 1]) {
int j = i;
int temp = toSort[i];
while (j > 0 && temp < toSort[j - 1]) {
toSort[j] = toSort[j - 1];
j--;
}
toSort[j] = temp;
}
}
} public static void main(String[] args) {
for (int j = 0, n = 2500; j < 3; j++, n = n * 10) {
System.out
.println("**************************************************");
System.out.println("Number to Sort is:" + n);
int[] array = CommonUtils.getRandomIntArray(n, 1000000);
System.out.print("Array to sort is:");
CommonUtils.printIntArray(array); int[] array1 = Arrays.copyOf(array, n);
new InsertionSort().sortAndprint(array1);
}
}
}

InsertionSort.java

 package com.cnblogs.riyueshiwang.sort;

 import java.text.MessageFormat;

 public abstract class abstractSort {
/**
*
* @param toSort
* array to sort
*/
protected abstract void sort(int[] toSort); public void sortAndprint(int[] toSort) {
Long begin = System.currentTimeMillis();
sort(toSort);
Long end = System.currentTimeMillis();
System.out.println(MessageFormat.format(
"Cost time of 【{0}】 is(milliseconds):{1}", this.getClass()
.getSimpleName(), (end - begin)));
System.out.print(MessageFormat.format("Sort result of 【{0}】:", this
.getClass().getSimpleName()));
CommonUtils.printIntArray(toSort);
} }

abstractSort.java

 package com.cnblogs.riyueshiwang.sort;

 import java.util.Random;

 public class CommonUtils {
private static Random random = new Random(); public static void printIntArray(int[] array) {
System.out.print('{'); int length = Math.min(array.length, 10);
for (int i = 0; i < length; i++) {
System.out.print(array[i]);
if (i != length - 1) {
System.out.print(',');
} else {
if (array.length > 10) {
System.out.print("...");
}
System.out.println('}');
}
}
} public static int[] getRandomIntArray(int size, int maxValue) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = random.nextInt(maxValue);
}
return array;
} public static void swap(int[] toSort, int i, int j) {
int temp = toSort[i];
toSort[i] = toSort[j];
toSort[j] = temp;
}
}

CommonUtils.java

排序算法一:插入排序(Insertion sort)的更多相关文章

  1. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  2. 排序算法--插入排序(Insertion Sort)_C#程序实现

    排序算法--插入排序(Insertion Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来 ...

  3. 跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort)

    跳跃空间(链表)排序 选择排序(selection sort),插入排序(insertion sort) 选择排序(selection sort) 算法原理:有一筐苹果,先挑出最大的一个放在最后,然后 ...

  4. 排序算法 - 插入排序(Insertion sort)

    插入排序对于少量元素的排序是很高效的,而且这个排序的手法在每个人生活中也是有的哦. 你可能没有意识到,当你打牌的时候,就是用的插入排序. 概念 从桌上的牌堆摸牌,牌堆内是杂乱无序的,但是我们摸上牌的时 ...

  5. [算法] 插入排序 Insertion Sort

    插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常采用in-pla ...

  6. [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  7. 插入排序Insertion Sort

    插入排序:将一个数据插入到一个已经排好序的有序数据序列中,从而得到一个新的.个数+1的有序数列:插入排序适用于少量数据排序,时间复杂度为O(n^2). 实现思路:1.对于一个无序数组,选取第一个元素, ...

  8. 插入排序 Insertion Sort

    插入排序算法的运作如下: 通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 插入排序算法的实现我放在这里. 时间/空间复杂度: 最差时间复杂度 O(n^2) 最优时间 ...

  9. 插入排序——Insertion Sort

    基本思想: 在要排序的一组数中,假定前n-1个数已经排好序,现在将第n个数插到前面的有序数列中,使得这n个数也是排好顺序的.如此反复循环,直到全部排好顺序. 过程: 平均时间复杂度:O(n2) jav ...

随机推荐

  1. JVM调优 — 命令大全(jps jstat jmap jhat jstack jinfo)(转)

    运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...

  2. 2019 Multi-University Training Contest 7 - 1006 - Snowy Smile - 线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=6638 偷学一波潘哥的二维离散化和线段树维护最大子段和. 思路是枚举上下边界,但是不需要从左到右用最大子段和dp. ...

  3. Java OO知识总结

    接着上一集    https://www.cnblogs.com/pdev/p/11288014.html       5. OO中的一些基本概念 继承 父类的非private方法自动被子类继承 cl ...

  4. NancyFx 2.0的开源框架的使用-ConstraintRouting

    新建一个空的Web项目 然后在Nuget库中安装下面两个包 Nancy Nancy.Hosting.Aspnet 然后在根目录添加三个文件夹,分别是models,Module,Views 然后往Mod ...

  5. Dorado环境启动出错Spring加载不到资源Bean配置 at org.springframework.asm.ClassReader.<init>(Unknown Source)

    ERROR: org.springframework.web.context.ContextLoader - Context initialization failed org.springframe ...

  6. Android Studio 于夜神模拟器进行连接

    本文使用夜神模拟器自带的nox_adb.exe在Android Studio中连接夜神模拟器. 1.在夜神模拟器的安装路径下,在bin文件夹下有个nox_adb.exe文件,复制黄色框路径如图: 2. ...

  7. jsp页面转换时间戳

    <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <fmt:fo ...

  8. ES常见错误

    1. Request cannot be executed; I/O reactor status: STOPPED RestClient被关闭了 2. SpringBoot启动后 Stopping ...

  9. 学Python的第七天

    今天学习DBA有点乏,所以Python学的不是很多熬!!! 但是不管多苦多累Python不会放弃!! 虽然我是运维! #!/usr/bin/env python3 # -*- coding:utf-8 ...

  10. 通过进程id找到进程对应的容器并统计每个进程的内存占用写到excel里

    # coding=utf-8 import re import os import commands import json import psutil from pyExcelerator impo ...