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

文章分几篇讲经典排序算法,直接上代码,根据结果对算法性能有个直观了解。本篇先说插入排序(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. Python pass是空语句用法

    在条件判断,还是函数中,有时候不需要输出任何东西,也不能留空,python提供空的语句,下面讲述pass空语句的用法 1,关键词 pass 2,用法 for letter in 'Python': i ...

  2. Neo4j 修改关系类型 type

    没有直接修改的函数,也不需要,下面代码就可以: MATCH (n:User {name:"foo"})-[r:REL]->(m:User {name:"bar&qu ...

  3. Robot Framework 源码阅读 day2 TestSuitBuilder

    接上一篇 day1 run.py 发现build test suit还挺复杂的, 先从官网API找到了一些资料,可以看出这是robotframework进行组织 测试案例实现的重要步骤, 将传入的te ...

  4. OpenVINO 安装及使用

    安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...

  5. 网络拓扑_VLAN与Trunk配置

    实验目的: 1.实现VLAN10的两台主机互通; VLAN20的两台主机互通; 2.VLAN10与VLAN20主机不能互通. 拓扑图: 步骤: 1.依图配置PC1,PC2,PC3,PC4的IP,掩码, ...

  6. shell脚本--expect自动应答

    expect自动应答  TCL语言 需求1:远程登录到A主机,什么事情也不做 #! /usr/bin/env expect # 开启一个程序 spawn ssh root@192.144.213.11 ...

  7. alert(1) to win 16

  8. cmd优化

    cmd很方便,很简约,但是看久了也会视觉疲劳 因此我们来优化一下cmd 原博文是我的一位同学写的,链接如下:https://www.cnblogs.com/ljsh/p/10877715.html 打 ...

  9. wrapper配置文件详解

    参考资料 http://www.tuicool.com/articles/jqMv2q 文件编码,每个配置文件起始位置必须指定该文件的编码格式 encoding=UTF-8 如果包含配置文件出现问题可 ...

  10. 027:for标签使用详解

    for标签使用详解: for...in... 标签: for...in... 类似于 Python 中的 for...in... .可以遍历列表.元组.字符串.字典等一切可以遍历的对象.示例代码如下: ...