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. linux安全组配置

    万网的是这样子配置的:

  2. MySQL:ERROR 1067 (42000): Invalid default value for 'end_time'

    © 版权声明:本文为博主原创文章,转载请注明出处 1.错误截图 2.错误分析 表中的第一个TIMESTAMP列(如果未声明为NULL或显示DEFAULT或ON UPDATE子句)将自动分配DEFAUL ...

  3. Lua学习十----------Lua数组

    © 版权声明:本文为博主原创文章,转载请注明出处 1.LUA数组 - 一维数组 - 多维数组 2.array.lua -- 一维数组 print("一维数组") array1 = ...

  4. 基于vue + axios + lrz.js 微信端图片压缩上传

    业务场景 微信端项目是基于Vux + Axios构建的,关于图片上传的业务场景有以下几点需求: 1.单张图片上传(如个人头像,实名认证等业务) 2.多张图片上传(如某类工单记录) 3.上传图片时期望能 ...

  5. 跟我一起写 Makefile(三)[转]

    原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) make 的运行—————— 一 ...

  6. [译]GLUT教程 - 动画

    Lighthouse3d.com >> GLUT Tutorial >> Basics >> Animation 前面章节我们已经创建了一个白色三角形的窗体.还没到 ...

  7. Ionic项目打包安卓APK

    之前用Ionic+Angular做了几个小应用Demo,现在用其中一个做实验试下打包安卓的APK安装包.(备注:我用的应用demo是之前博客里写的汇率的Demo,不清楚的同学可以查哈~) 我是用ion ...

  8. Mysql----MySQL的mysql_insert_id和LAST_INSERT_ID(转)

    本文介绍的是mysql中last_insert_id和mysql_insert_id的区别 1 mysql_insert_id 一.PHP获取MYSQL新插入数据的ID mysql_insert_id ...

  9. laravel学习之路2: jwt集成

    "tymon/jwt-auth": "^1.0@dev", 执行 composer update 'providers' => [ .... Tymon\ ...

  10. tornado+ansible+twisted+mongodb运维自己主动化系统开发(四)

    这周好忙,依据之前的写了个简陋的demo.放在腾讯的云主机上了,大家多交流哈 demo地址 http://203.195.193.251/