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. vi编辑器命令大全

    >> from zhuhaiqing.info

  2. android 常用方法总结

    public class Toolkit { /** * * Role:Telecom service providers获取手机服务商信息 <BR> * * 需要加入权限<uses ...

  3. TSharding源码阅读

    需要的背景知识:Spring 和Mybatis 实现原理和源码, javaassist字节码增强的使用, java及设计模式的使用 1 读取解析数据库配置文件 DataSourceScanner实现了 ...

  4. 下载某资源文件并加载其中的所有Prefab到场景中

    using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...

  5. 三种光照模型的shader实现

    1.Lambert模型,公式为I=Kd*Il(N*L): Shader "Custom/Lambert_A" { Properties { _Diffuse(,,,) } SubS ...

  6. iOS base64加密解密

    本文转载至 http://jingyan.baidu.com/article/93f9803fff45c9e0e46f5596.html 从参考资料的地址中下载GTMBase64.zip库文件包,并解 ...

  7. 【BZOJ3060】[Poi2012]Tour de Byteotia 并查集

    [BZOJ3060][Poi2012]Tour de Byteotia Description 给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上. Input     ...

  8. H - Coins

    H - Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  9. 使用tomcat7-maven-plugin部署Web项目

      一.环境准备 我使用的环境是:Window 10.Tomcat 8.0.36.maven3.tomcat7-maven-plugin 2.2版本. 二.设置环境变量 安装Tomcat8.0.36和 ...

  10. Orthogonal Least Squares Learning Algorithm for Radial Basis Function Networks

    Orthogonal Least Squares Learning Algorithm for Radial Basis Function Networks S. Chen, C. F. N. Cow ...