算法(Algorithms)第4版 练习 2.1.25
代码实现:
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class InsertionHalfExchange { public static void sort(Comparable[] a) { int N = a.length; for(int i = 1; i < N; i++) {
Comparable temp = a[i];
int j = i;
while(j > 0 && less(temp, a[j-1])) {
a[j] = a[j-1];
j--;
}
a[j] = temp;
// 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);
} }
单元测试结果:
S O R T E X A M P L E
O S R T E X A M P L E
O R S T E X A M P L E
O R S T E X A M P L E
E O R S T X A M P L E
E O R S T X A M P L E
A E O R S T X M P L E
A E M O R S T X P L E
A E M O P R S T X L E
A E L M O P R S T X E
A E E L M O P R S T X
A E E L M O P R S T X
比较Insertion和InsertionHalfExchange 的性能:
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("Shell"))
Shell.sort(a); return timer.elapsedTime(); } public static void main(String[] args) { String alg1 = "Insertion";
String alg2 = "InsertionHalfExchange";
int N = 1000;
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 1000 random Doubles 1000 trials
Insertion is 1.1s InsertionHalfExchange is 0.5s
算法(Algorithms)第4版 练习 2.1.25的更多相关文章
- 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# 习题题解——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搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- 获取 Android 版本
var a = /Android(.+?);/.exec(window.navigator.userAgent) if (a) { this.AndroidVersion = +(a[1].trim( ...
- docker教程之从一头雾水到不一头雾水(1)
一.安装docker 1.搜索docker [root@node3 ~]# yum search docker Loaded plugins: fastestmirror, langpacks Loa ...
- 第二章:核心C#
变量 如果在一条语句中声明和初始化了多个变量,那么所有的变量都具有相同的数据类型 Int x=10,y=20: 变量使用前需要赋初始值 声明不同类型的变量需要单独的语句. 变量的初始化 变量时类或者结 ...
- csv文件中出现乱码的解决方法
1.首先用UE打开CSV文件,发现没有乱码了. 2.然后新建一个txt文本,把CSV中的数据复制到txt文本中,保存格式为ANSI/ASCII. 3.复制txt文件,再把副本后缀改为CSV格式,再用E ...
- 【数据挖掘】关联分析之Apriori(转载)
[数据挖掘]关联分析之Apriori 1.Apriori算法 如果一个事务中有X,则该事务中则很有可能有Y,写成关联规则 {X}→{Y} 将这种找出项目之间联系的方法叫做关联分析.关联分析中最有名的问 ...
- Linux驱动platform
platform device<==> platform bus <==> platform driver 转自:platform设备驱动全透析 宋宝华 http://blog ...
- a byte of vim -- 学习摘要
说在前面的话 -- a byte of vim 是我见过的最介绍vim 最好的书,想了解强大的vim的人,或者是已经在使用vim而打算进一步了解的人,我感觉都应该看看这个,内容精炼但涵盖非常广,--& ...
- cocoapods 错误处理
error: RPC failed; curl 56 SSLRead() return error -36 [!] /usr/bin/git clone https://github.com/Coco ...
- c/c++的一些小知识点2
- 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2
P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...