算法(Algorithms)第4版 练习 2.2.11(最终)
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeX { private static Comparable[] aux;
private final static int CUTOFF = 8;//size public static void sort(Comparable[] input) {
int N = input.length;
aux = input.clone();//must be clone(the same as input)
StdOut.println("input:" + input + " aux:" + aux);//for test
sort(aux, input, 0, N-1);
} //this level takes source as input(need to be sorted)
//and destination as auxiliary, and put the result in destination
private static void sort(Comparable[] source, Comparable[] destination, int lo, int hi) {//avoid copy if((lo+CUTOFF-1) >= hi) { //use insertion sort for tiny subarrays
insertionsort(destination, lo, hi);//prepare destination for up level
return;
} int mid = lo + (hi-lo)/2;
sort(destination, source, lo, mid);//down level switch the roles of the input array and auxiliary array
sort(destination, source, mid+1, hi); if(!less(source[mid+1], source[mid])) {//ship merge
System.arraycopy(source, lo, destination, lo, hi-lo+1);//prepare destination for up level
StdOut.println("destination:" + destination);//for test
StdOut.printf("skip merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);//for test
show(destination);//for test
return;
} merge(source, destination, lo, mid, hi);//merge sorted source to destination } private static void insertionsort(Comparable[] input, int lo, int hi) {
for(int i = lo + 1; i <= hi; i++) {
for(int j = i; j > lo && less(input[j], input[j-1]); j--) {
exch(input, j, j-1);
}
} StdOut.println("destination:" + input);
StdOut.printf("insertionsort(input, %4d, %4d)", lo, hi);//for test
show(input);//for test
} private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i];
a[i] = a[j];
a[j] = t; } private static void merge(Comparable[] source, Comparable[] destination, int lo, int mid, int hi) { int i = lo;
int j = mid + 1;
for(int k = lo; k <= hi; k++) {
if(i > mid)
destination[k] = source[j++];
else if(j > hi)
destination[k] = source[i++];
else if(less(source[j], source[i]))
destination[k] = source[j++];
else
destination[k] = source[i++];
}
StdOut.println("source:" + source + " destination:" + destination);//for test
StdOut.printf("merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);//for test
show(destination);//for test } private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } 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[] input = In.readStrings();
show(input);//for test
sort(input);
assert isSorted(input);
show(input);//for test } }
测试:
M E R G E S O R T E X A M P L E
input:[Ljava.lang.String;@1b6d3586 aux:[Ljava.lang.String;@4554617c
destination:[Ljava.lang.String;@4554617c
insertionsort(input, 0, 7)E E G M O R R S T E X A M P L E
destination:[Ljava.lang.String;@4554617c
insertionsort(input, 8, 15)E E G M O R R S A E E L M P T X
source:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@1b6d3586
merge(source, destination, 0, 7, 15)A E E E E G L M M O P R R S T X
A E E E E G L M M O P R R S T X
性能比较:
For 20000 random Doubles 1000 trials
Merge is 3.6s
MergeFasterM is 3.1s
MergeUseInsert is 3.2s
MergeSkipMerge is 3.5s
MergeAvoidCopy is 3.0s
MergeX is 2.9s
算法(Algorithms)第4版 练习 2.2.11(最终)的更多相关文章
- 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 ...
随机推荐
- 解决ubuntukylin 13.10安装wine时无法解决软件包依赖问题
在ubuntukylin 13.10中,无论是在软件中心安装wine还是通过apt-get install安装wine都会出现软件包依赖问题且无法解决. 问题重现 在软件中心安装wine报错例如以下: ...
- 分析Memcached客户端如何把缓存数据分布到多个服务器上
Memcached客户端可以设多个memcached服务器,它是如何把数据分发到各个服务器上,而使各个服务器负载平衡的呢? 可以看看.net版中的客户端中的源码,就可以知道 先看代码: 1 /// ...
- asp.net web.config配置节说明
web.config 文件查找规则: (1)如果在当前页面所在目录下存在web.config文件,查看是否存在所要查找的结点名称,如果存在返回结果并停止查找. (2)如果当前页面所 ...
- vim-colors-config
在vim中,主题是以插件形式存在.其中系统自带的主题,存放在$VIMRUNTIME/colors文件夹下,以*.vim命名.(注:查看$VIMRUNTIME请在vim中执行 :echo $VIMRUN ...
- k8s部署dashborad
环境: 两台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-scheduler ...
- 【转】【Python + selenium】linux和mac环境,驱动器chromedriver和测试报告HTMLTestRunner放置的位置
感谢: 作者:gz_tester,文章:<linux和mac环境,chromedriver和HTMLTestRunner放置的位置> 使用场景 配置python selenium 环境 使 ...
- Android实现点击通知栏后,先启动应用再打开目标Activity ,极光推送等推送的也可以参考一下(转)
我因为项目中集成了极光推送,推送的通知栏点开需要确定进入哪个界面就参考了这边文章,感谢作者的无私. 标签: 情况简述 在开发Android app的过程中,遇到这样一个需求:app中启动一个Servi ...
- python之开篇---hello world!
(1)前沿 (2)python 简介 (3)python hello world 实现 (4) -------------qq:1327706646 ------------------------- ...
- angular选择器功能
1.$event对象 $event对象其实就是潜在的jQuery事件对象,通过$event.currentTarget获取当前元素,通过$event.target获取当前元素的子元素. 例如: ...
- unity shader学习笔记(1) shader基础结构以及Properties面板
首先是shader的基础结构: Shader "Custom/Example { Properties//变量属性面板 { } SubShader { Tags { "Render ...