在sort函数创建aux数组:

package com.qiusongde;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeNoStaticArray { public static void sort(Comparable[] input) {
int N = input.length;
Comparable[] aux = new Comparable[N];
sort(input, aux, 0, N-1);
} private static void sort(Comparable[] input, Comparable[] aux, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(input, aux, lo, mid);
sort(input, aux, mid+1, hi);
merge(input, aux, lo, mid, hi); } private static void merge(Comparable[] input, Comparable[] aux, int lo, int mid, int hi) { //copy input[lo,hi] to aux[lo,hi]
for(int i = lo; i <= hi; i++) {
aux[i] = input[i];
} int i = lo;
int j = mid + 1;
for(int k = lo; k <= hi; k++) {
if(i > mid)
input[k] = aux[j++];
else if(j > hi)
input[k] = aux[i++];
else if(less(aux[j], aux[i]))
input[k] = aux[j++];
else
input[k] = aux[i++];
} StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi);
show(input);//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 } }

在merge函数创建aux数组:

package com.qiusongde;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeArrayCreation { public static void sort(Comparable[] input) {
int N = input.length;
sort(input, 0, N-1);
} private static void sort(Comparable[] input, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(input, lo, mid);
sort(input, mid+1, hi);
merge(input, lo, mid, hi); } private static void merge(Comparable[] input, int lo, int mid, int hi) { //copy input[lo,hi] to aux[lo,hi]
Comparable[] aux = new Comparable[hi-lo+1];
int auxmid = mid - lo;
int auxhi = hi - lo;
for(int i = lo; i <= hi; i++) {
aux[i - lo] = input[i];
} int i = 0;//lo in aux array
int j = (auxmid) + 1;//mid + 1 in aux array
for(int k = lo; k <= hi; k++) {
if(i > auxmid)
input[k] = aux[j++];
else if(j > auxhi)
input[k] = aux[i++];
else if(less(aux[j], aux[i]))
input[k] = aux[j++];
else
input[k] = aux[i++];
} StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi);
show(input);//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 } }

性能对比:

package com.qiusongde;

import edu.princeton.cs.algs4.StdOut;

public class Exercise2226 {

    public static void main(String[] args) {
String alg1 = "MergeNoStaticArray";
String alg2 = "MergeArrayCreation";
int N = 20000;
int T = 1000; double t1 = SortCompare.timeRandomInput(alg1, N, T);
double t2 = SortCompare.timeRandomInput(alg2, N, T); StdOut.printf("For %d random Doubles %d trials\n", N, T, alg1);
StdOut.printf("%s is %.1fs %s is %.1fs", alg1, t1, alg2, t2);
} }
For 20000 random Doubles 1000 trials
MergeNoStaticArray is 3.3s MergeArrayCreation is 3.9s

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

  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. php SSL certificate problem: unable to get local issuer certificate

    加上 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); 就可以了 百度语音的demo: <?php header("Content-type ...

  2. Maven环境下搭建SSH框架之Spring整合Hibernate

    © 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...

  3. AccessibilityService 官网介绍

    AccessibilityService extends Service java.lang.Object    ↳ android.content.Context      ↳ android.co ...

  4. 大师养成计划之二:hibernate框架的使用------实例演示

    搭建hibernate项目框架的步骤: 一.导入jar包 二.new    .cfg.xml配置文件 <?xml version="1.0" encoding="U ...

  5. erlang 最大公约数

    一般面试会遇到问一些算法,什么排序,树,图等等,冷不丁还会问几个蛋疼的问题,我估计生产情况十有八九都用不上,只是题目罢了. 题目:求两个大数的最大公约数. 什么是最大公约数呢? 百度百科的答案这样的: ...

  6. python 学习2 测试报告

    1. py.test test_class.py  --resultlog=./log.txt 2.生成html格式 py.test test_class.py --html=./report.htm ...

  7. 关于使用eclipse开发最小运行组件包

    有的时候向用eclipse组件,但是其中好多东西是相互关联的,如果在eclipse上做二次开发固然可以,但是有的时候想要的只不过是一个可以运行的架包而已,所以不必要那么多东西. 下面是我使用eclip ...

  8. WPF Button TextBox 圆角

    <!--圆角button--> <Style TargetType="Button"> <Setter Property="FontSize ...

  9. 【POJ-2524】Ubiquitous Religions(并查集)

    并查集. #include<cstdio> #include<cstring> using namespace std; const int maxn = 55555; int ...

  10. LINQ分页工具

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...