关键代码实现:

  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. 关于CDN对动态网站加速的一些看法

    CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络"边缘" ...

  2. Unity多个场景叠加或大场景处理方法小结

    本文章由cartzhang编写.转载请注明出处. 全部权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/47614153 作者:ca ...

  3. VM虚拟机内ubuntu无法连接到网络

    VM虚拟机内ubuntu无法连接到网络 解决:编辑网络,将网路都删除掉.又一次加入网络桥接和NAT链接. .又一次连接就可以,查看一下ip地址. 方法2: 虚拟机中新装ubuntu 编辑虚拟网络,先恢 ...

  4. VMware网络连接 桥接、NAt、host-only模式

    如果你想利用VMWare安装虚拟机,或想创建一个与网内其他机器相隔离的虚拟系统,进行特殊的调试工作.此时,对虚拟系统网络连接模式的选择就非常重要了.如果你选择的工作模式不正确,就无法实现上述目的,也就 ...

  5. php判断页面是否是微信打开的示例(微信打开网页)

    代码如下: $user_agent = $_SERVER['HTTP_USER_AGENT'];if (strpos($user_agent, 'MicroMessenger') === false) ...

  6. xshell 连接腾讯服务器

    1.先关机, 创建秘钥,再绑定主机,下载秘钥保存下来 2. 填写好主机好和端口 3 4.导入刚才下载的文件 记住用户名是ubuntu 不是root!!

  7. 线段树专题—HDU1698 Just a Hook

    题意:t组数据,给一个n.m表示n长度的钩和m次操作.初始钩子的每单位长度的价值为1,接下来输入 x,y,k 的操作把钩子[x,y]区间的价值替换为k,求m次操作后钩子的价值为多少 分析:成段替换.最 ...

  8. 如何通过Google访问外网

    修改host: https://laod.cn/hosts/2017-google-hosts.html google中文: https://www.google.com.hk/ 弄好前两项后,可以再 ...

  9. POJ 1860

    须要推断是否有正权环存在,Bellman-Ford算法就能够辣~ AC代码: #include <iostream> #include <cstdio> #include &l ...

  10. iOS-Core-Animation-Advanced-Techniques(二)

    本文转载至 http://www.cocoachina.com/ios/20150104/10816.html 视觉效果和变换 (四)视觉效果 嗯,园和椭圆还不错,但如果是带圆角的矩形呢? 我们现在能 ...