代码实现:

package com.qiusongde;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class InsertionWithSentinel { public static void sort(Comparable[] a) { int N = a.length; for(int i = N - 1; i > 0; i--) {
if(less(a[i], a[i-1])) {
exch(a, i, i-1);
// show(a);
}
} for(int i = 2; i < N; i++) {
for(int j = i; less(a[j], a[j-1]); j--) {
exch(a, j, j-1);
}
// show(a);
} } private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i];
a[i] = a[j];
a[j] = t; } private static void show(Comparable[] a) { //print the array, on a single line.
for(int i = 0; i < a.length; i++) {
StdOut.print(a[i] + " ");
}
StdOut.println(); } public static boolean isSorted(Comparable[] a) { for(int i = 1; i < a.length; i++) {
if(less(a[i], a[i-1]))
return false;
} return true; } public static void main(String[] args) {
//Read strings from standard input, sort them, and print.
String[] a = In.readStrings();
show(a);
sort(a);
assert isSorted(a);
show(a);
} }

SortCompare:

package com.qiusongde;

import edu.princeton.cs.algs4.Shell;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom; public class SortCompare { public static double timeRandomInput(String alg, int N, int T) { double total = 0.0; Double[] a = new Double[N];
for(int t = 0; t < T; t++) {
for(int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
total += time(alg, a);
} return total; } public static double time(String alg, Double[] a) { Stopwatch timer = new Stopwatch(); if(alg.equals("Selection"))
Selection.sort(a);
if(alg.equals("Insertion"))
Insertion.sort(a);
if(alg.equals("InsertionHalfExchange"))
InsertionHalfExchange.sort(a);
if(alg.equals("InsertionWithSentinel"))
InsertionWithSentinel.sort(a);
if(alg.equals("Shell"))
Shell.sort(a); return timer.elapsedTime(); } public static void main(String[] args) { String alg1 = "Insertion";
String alg2 = "InsertionWithSentinel";
int N = 2000;
int T = 1000; double t1 = timeRandomInput(alg1, N, T);
double t2 = timeRandomInput(alg2, N, T); StdOut.printf("For %d random Doubles %d trials\n", N, T, alg1);
StdOut.printf("%s is %.1fs %s is %.1fs", alg1, t1, alg2, t2); } }

测试结果:

For 2000 random Doubles 1000 trials
Insertion is 4.5s InsertionWithSentinel is 3.7s

最新文章

  1. java多线程详解(6)-线程间的通信wait及notify方法
  2. mongo db安装和php,python插件安装
  3. 基于visual Studio2013解决算法导论之027hash表
  4. Xutils呼叫流源代码文件下载方法
  5. PHP代理访问网络资源
  6. c# RSA加密和解密
  7. 【原创】抓个Firefox的小辫子,围观群众有:Chrome、Edge、IE8-11
  8. 使用supervisor 进行进程管理时调整最大文件打开数
  9. [SqlServer]如何向数据库插入带有单引号(&#39;)的字符串
  10. javascript获取当前域名
  11. Python- redis缓存 可达到瞬间并发量10W+
  12. hbase 概念
  13. 从零开始学安全(十)●TCP/IP协议栈
  14. ABP框架系列之四:(Repositories-仓库)
  15. Scala字符串插值
  16. python 创建flask项目方法
  17. spring 之 lookup-method &amp; replace-method
  18. 树莓派上的软件安装和卸载命令汇总 [ZT]
  19. libxml/HTMLparser.h file
  20. [svc]为何linux ext4文件系统目录默认大小是4k?

热门文章

  1. C#:异步编程和线程的使用(.NET 4.5 ),异步方法改为同步执行
  2. .net验证控件,导航控件
  3. 《TomCat与Java Web开发技术详解》(第二版) 第三章节的学习总结--利用Context元素来自定义web应用的存储位置
  4. thinkPHP5.0的学习研究【序言】
  5. Mockito when(...).thenReturn(...)和doReturn(...).when(...)的区别
  6. Razor里写函数
  7. Python小白的发展之路之Python基础(二)【字符串、列表、集合、文件操作】
  8. 记录-spring MultipartFile 文件上传
  9. mybatis generator的用法
  10. NOI-linux下VIM的个人常用配置