算法(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搭建的教程 ...
随机推荐
- 利用Bootstrap简单实现一个文件上传进度条
© 版权声明:本文为博主原创文章,转载请注明出处 说明: 1. 使用commons-fileupload.jar实现文件上传及进度监听 2. 使用bootstrap的进度条进行页面显示 3. 因为进度 ...
- zoj 3827 Information Entropy 【水题】
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
- ES 31 - 从0开始搭建Elasticsearch生产集群
目录 1 配置环境 1.1 服务器IP映射 1.2 配置各节点的ssh免密通信 1.3 安装JDK并配置环境变量 2 部署单节点服务 3 部署集群服务 4 启动集群中的所有节点 4.2 启动各个节点中 ...
- 《HBase in Action》 第三章节的学习总结 ---- 如何编写和运行基于HBase的MapReduce程序
HBase之所以与Hadoop是最好的伙伴,我理解就因为两点:1.HADOOP的HDFS,为HBase提供了分布式的存储方式:2.HADOOP的MR为HBase提供的分布式的计算方法.u 其中第一点, ...
- Auto Layout之创建布局约束
上篇文章给大家介绍了AutoLayout 的由来及OC中关于AutoLayout 的类.这篇文章将向大家介绍,在iOS 编程中怎样使用Auto Layout 构建布局约束. 创建布局约束 创建布局约束 ...
- rebound是facebook的开源动画库
网址:http://www.jcodecraeer.com/a/opensource/2015/0121/2338.html 介绍: rebound是facebook的开源动画库.可以认为这个动画库是 ...
- 多媒体开发之--- live555 vs2010/vs2013下编译,使用,测试
Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现了对多种音视频编 ...
- PHP中使用POST发送请求的常用方式
前提概要: 在PHP进行项目开发过程中,使用post发送请求的情况很多,以下总结了项目中主要用的两种方式. 总结下: 在我接触到的项目中用到第二种情况较多,比如写:短信接口.....总体来说比较简单便 ...
- 使用MYCAT轻松实现MYSQL水平分片
完整文章下载地址:http://download.csdn.net/detail/dreamcode/9383516 简单来说,我们能够将数据的水平切分理解为是依照数据行的切分.就是将表中的某些行切分 ...
- Android 新建一个类,在src新建一个类,使继承自活动
一:先新建一个包 右键src,新建包: 二:包中新建类 右建包,新建类,将超类改为andorid.app.Activity