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

文章分几篇讲经典排序算法,直接上代码,根据结果对算法性能有个直观了解。本篇先说插入排序(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. Gradle Could not find method leftShift() for arguments

    task hello << { println 'Hello world!' } 其中 << 在gradle 在5.1 之后废弃了 可以查看gradle 版本号 gradle ...

  2. 安装python3并安装pip3

    python是一门高级编译语言,这么语言可以让你做一些运维平台,是因为他可以执行linux中的命令,让你实现自动化和半自动话,s 在运维开发这方面的话,就相当于把shell和java给结合了一下,ja ...

  3. [小试牛刀]部署在IDEA的JFinal 3.0 demo

    进入JFinal 极速开发市区:http://www.jfinal.com/ 如上图,点击右边的最新下载:JFinal 3.0 demo - 此过程跳过注册\登录过程, 进入到如下,下载 下载并解压到 ...

  4. hr员工数据分析(实战)

    hr员工数据分析项目实战 (数据已脱敏) 背景说明 某公司最近公司发生多起重要员工意外离职.部分员工工作缺乏积极性等问题,受hr部门委托,开展数据分析工作. 经与hr部门沟通,确定以下需求: 制定数据 ...

  5. MFC消息详解 (WindowProc|OnCommand|OnNotify)

    1. 怎样使用MFC发送一个消息用MFC发送一个消息的方法是, 首先,应获取接收消息的CWnd类对象的指针: 然后,调用CWnd的成员函数SendMessage( ). LRESULT Res=pWn ...

  6. mongodb 用户 权限 设置 详解

    原文地址:http://blog.51yip.com/nosql/1575.html 我知道的关系型数据库都是有权限控制的,什么用户能访问什么库,什么表,什么用户可以插入,更新,而有的用户只有读取权限 ...

  7. selenium 自动化的坑(2)

    UI自动化,一天一坑系列(2) 今天要介绍的坑是这样的:在使用google浏览器的过程中,F12查看页面元素,我的操作步骤是先F12,然后点击箭头,接着点击要查找的元素来实现元素查看,不知道你是不是这 ...

  8. Android Release 打包提示 "错误:找不到符号"

    搞了一上午....必须记录下来

  9. 026:if标签使用详解

    if标签使用详解: if 标签: if 标签相当于 Python 中的 if 语句,有 elif 和 else 相对应,但是所有的标签都需要用标签符号  {%  %}  进行包裹. if 标签中可以使 ...

  10. win10下配置多个mysql数据库

    mysql正常安装步骤:下载安装参考: 我配置的时8.0.13和5.7.27这两个版本: 配置完第一个数据库之后:复制ini文件给第二个数据库注意修改文件的端口时,先确认端口是否被占用 [mysql] ...