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. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. Mongodb 的学习

    传送门: # 官方网站 及 下载地址 https://www.mongodb.com/download-center/enterprise/releases # 之前简单学习的笔记http://www ...

  2. JavaFX打包到Android上

    让JavaFX执行到移动平台一直是社区努力完毕的事.  当然,眼下已经能够让JavaFX执行到Android和IOS平台了,以下我们来看看怎样打包自己的JavaFX项目到Android平台.  首先下 ...

  3. ubuntu打开终端多开标签的快捷键是ctrl+ shift+ T 对比ctrl+ alt+ T 另外窗口打开一个终端

    ubuntu打开终端多开标签的快捷键是ctrl+ shift+ T 对比ctrl+ alt+ T 另外窗口打开一个终端

  4. 利用SQL server 的复制功能分散用户访问服务器的负载

    先来了解一下一个基本的关于复制的概念. 什么是复制? 复制就是把数据的多个拷贝(复制品)分发到公司中的各个服务器中,通过复制为多台服务器提供相同的数据.这样用户就可以在不同服务器中访问同样的信息. 对 ...

  5. CAP理论学习

    CAP理论是对分布式系统的3个特性所下的一个定性的结论,可用于指导分布式系统的设计. CAP理论断言任何基于网络的数据共享系统,最多只能满足数据一致性.可用性.分区容忍性三要素中的两个要素. 在英语中 ...

  6. 《TomCat与Java Web开发技术详解》(第二版) 第八章节的学习总结 -- 访问mysql

    终于学到如何访问Mysql了 1. 可以看看此章节提供的sql脚本,以后可以照着写了.此外,对于Mysql如何使用,最好的地方就是其官网介绍了.http://dev.mysql.com/doc/ref ...

  7. linux centos apache开启gzip的方法

    开启gzip压缩的方法很简单,连接服务器并打开配置文件“httpd.conf”,找到下面这两句,去掉前面的“#”  代码如下 1 LoadModule deflate_module modules/m ...

  8. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  9. 浏览器登录cookie

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  10. hdu3068 最长回文(manacher 算法)

    题意: 给定字符串.求字符串中的最长回文序列 解题思路: manacher 算法 时间复杂度:O(N) 代码: #include <cstdio> #include <cstring ...