关键代码实现:

  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. 解决H5在微信浏览器或QQ浏览器修改title的问题

    传送门:http://blog.csdn.net/code_for_free/article/details/51195468 如果是Android,使用 document.title = ‘1231 ...

  2. JQuery小结(转)

    一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...

  3. Sphinx之配置文件

    # # Sphinx configuration file sample # # WARNING! While this sample file mentions all available opti ...

  4. distcc加速内核编译

    Linux内核编译实在是费时间的事,搞内核移植的时候总要编译,生命有一部分就浪费在等内核编译完成上,有心想买个HP的工作站,看了下Z840的价格,想想还是算了.distcc早就听说过,一直没有去试试, ...

  5. Hadoop相关资料

    http://blog.csdn.net/skywalker_only/article/details/40650427

  6. Redis源码阅读-Adlist双向链表

    Redis源码阅读-链表部分- 链表数据结构在Adlist.h   Adlist.c Redis的链表是双向链表,内部定义了一个迭代器. 双向链表的函数主要是链表创建.删除.节点插入.头插入.尾插入. ...

  7. xml初学简单介绍

    什么是XML? 1.全称Extensible Markup Language,可扩展标记语言,W3C组织公布. 2.XML用来保存有一定结构关系的数据. 3.标签的嵌套,实质是一串字符串. 4.跨平台 ...

  8. How can I detect multiple logins into a Django web application from different locations?

    1) Install django-tracking (thankyou for that tip Van Gale Google Maps + GeoIP is amazing!) 2) Add t ...

  9. iOS 蓝牙功能 bluetooth

    现将创建蓝牙工程的要点总结一下,由于工程主要涉及中心模式,所以只总结中心模式的用法 1,引入CoreBluetooth.framework 2,实现蓝牙协议,如: .h文件如下 @protocol C ...

  10. 调整虚拟机中Linux的分辨率

    1 在虚拟机配置中,将显示的缓存提高,CPU也提高. 2 运行Linux,在system-preferences-display中调整分辨率