关键代码实现:

  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);
} //take input from source, sort it and put output in destination
//source and destination is the same at the beginning
private static void sort(Comparable[] source, Comparable[] destination, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(destination, source, lo, mid);//first get input from destination, sort it and put output in source
sort(destination, source, mid+1, hi);
merge(source, destination, lo, mid, hi);//merge sorted source to destination } 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);
// StdOut.printf("merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);
// show(destination);//for test }

测试用例:

package com.qiusongde;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeAvoidCopy { private static Comparable[] aux; 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);
} //take input from source, sort it and put output in destination
//source and destination is the same in start
private static void sort(Comparable[] source, Comparable[] destination, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(destination, source, lo, mid);//first get input from destination, sort it and put output in source
sort(destination, source, mid+1, hi);
merge(source, destination, lo, mid, hi);//merge sorted source to destination } 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);
// StdOut.printf("merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);
// 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
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 0, 0, 1)E M R G E S O R T E X A M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 2, 2, 3)E M G R E S O R T E X A M P L E
source:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@1b6d3586
merge(source, destination, 0, 1, 3)E G M R E S O R T E X A M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 4, 4, 5)E M G R E S O R T E X A M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 6, 6, 7)E M G R E S O R T E X A M P L E
source:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@1b6d3586
merge(source, destination, 4, 5, 7)E G M R E O R S T E X A M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 0, 3, 7)E E G M O R R S T E X A M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 8, 8, 9)E E G M O R R S E T X A M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 10, 10, 11)E E G M O R R S E T A X M P L E
source:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@1b6d3586
merge(source, destination, 8, 9, 11)E G M R E O R S A E T X M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 12, 12, 13)E E G M O R R S E T A X M P L E
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 14, 14, 15)E E G M O R R S E T A X M P E L
source:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@1b6d3586
merge(source, destination, 12, 13, 15)E G M R E O R S A E T X E L M P
source:[Ljava.lang.String;@1b6d3586 destination:[Ljava.lang.String;@4554617c
merge(source, destination, 8, 11, 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.4s
MergeFasterM is 3.1s
MergeUseInsert is 3.2s
MergeSkipMerge is 3.3s
MergeAvoidCopy is 3.0s

算法(Algorithms)第4版 练习 2.2.11(3)的更多相关文章

  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. Windows安装Redis的php扩展

    Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不 ...

  2. 在Ubuntu 16.04下安装 virtualbox 5.2

        sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian xenial contrib" ...

  3. 学习IIS & MVC的运行原理

    我一直疑惑于以下问题,从客户端发出一个请求,请求到达服务器端是怎样跟iis衔接起来的,而iis又是怎样读取我发布的代码的,并返回服务器上的文件.这其中是怎样的一个处理过程. 1:当你从浏览器中输入一个 ...

  4. 启用nginx报错80端口被占用

    最近在本机跑nginx,启动后报错,怀疑80端口被占用 netstat -ano|findstr 尝试一:查看后发现端口被一个System pid 为4的一个程序占用.我在资源管理器中尝试将Syste ...

  5. PHP 学习内容

    第一阶段: (PHP+MySQL核心编程) 面向对象编程 MySQL数据库, MySQL的优化细节. HTTP协议,http也是我们web开发的基石.对我们了解PHP底层机制有很大帮助,做到知其然,还 ...

  6. UFLDL深度学习笔记 (六)卷积神经网络

    UFLDL深度学习笔记 (六)卷积神经网络 1. 主要思路 "UFLDL 卷积神经网络"主要讲解了对大尺寸图像应用前面所讨论神经网络学习的方法,其中的变化有两条,第一,对大尺寸图像 ...

  7. FMM和BMM的python代码实现

    FMM和BMM的python代码实现 FMM和BMM的编程实现,其实两个算法思路都挺简单,一个是从前取最大词长度的小分句,查找字典是否有该词,若无则分句去掉最后面一个字,再次查找,直至分句变成单词或者 ...

  8. shell脚本57问

    [1]交互方式.非交互方式.Shell脚本是什么? 经常与linux打交道,肯定对shell这个词不陌生.不明白shell意思的,可以自行翻译:外壳.去壳. 这个翻译结果怎么可以与计算机系统联系起来呢 ...

  9. Oracle Data Provider for .NET的使用(托管与非托管(一))

    目录 简单的概述 简单的使用 非托管系统要求 托管驱动系统要求 其它的注意事项 ODP.NET版本说明 安装ODP.NET 安装非托管驱动 非托管驱动绿色配置 简单的概述 ODP.NET的含义是 Or ...

  10. ios 深入讲解iOS键盘一:控制键盘隐藏显示

    在iOS的开发中,我们一般使用UITextField.UITextView处理文字输入等操作,大部分情况下我们只需要一两行代码去手动管理键盘的显示隐藏:让UITextField或UITextView成 ...