算法(Algorithms)第4版 练习 2.1.24
代码实现:
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
算法(Algorithms)第4版 练习 2.1.24的更多相关文章
- 1.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——2.4
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
随机推荐
- eclipse导入svn工程,在文件夹里面不展示svn工程图标
原因:没有安装subclipse插件 解决方法:在marketplace中搜索插件名字:subclipse,点击安装,或者在instal new software中输入插件名字,安装完成之后,在文件夹 ...
- Eval,Bind,<% %>,<%# %>和<%= %> 笔记
1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...
- windows upd广播包无法发送到局域网解决方法
不能发送广播包的电脑和可以发送广播报的主机对比,发现不能发送广播报的主机上都有安装虚拟机,也有虚拟网卡,将所有的虚拟网卡关闭,然后再进行测试,都正常了,无论是Win7,Win10还是Xp. 禁用VMw ...
- web开发之html5---html5 动画特效舞动的雨伞
http://www.cnblogs.com/stoneniqiu/p/4199294.html
- iOS 10 中引入了 Message 框架
WWDC 2016 上最重磅的消息之一就是在 iOS 10 中引入了 Message 框架.开发者现在可以为苹果内置的 Messages 应用开发扩展啦.通过开发一个应用扩展,你可以让用户跟应用在 M ...
- JS 创建对象(常见的几种方法)
贴个代码先: function O(user,pwd){ //use constructor this.user=user; this.pwd=pwd; this.get=get; return th ...
- Cow Contest(传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10450 Accepted: 5841 Desc ...
- WCF基础之序列化
wcf是基于消息进行通信的,这篇就是简单说下序列化引擎是如何将.net object转化为xml消息.一般情况下很少用到这些,你只需定义数据协定之类的或者指定相应的序列化引擎,然后设置相应的特性就好. ...
- 什么是Mocking framework?它有什么用?(转)
今天我想讲下关于mocking frameworks,并且解释下他为什么有用处.我将给你们展示用和不用mocking framework两种测试方法. 假设我们已经有了一个Driver类: publi ...
- css3 transition效果
<meta charset="UTF-8"> <style> .btn { display: inline-block; font-size: 12px; ...