在sort函数创建aux数组:

  1. package com.qiusongde;
  2.  
  3. import edu.princeton.cs.algs4.In;
  4. import edu.princeton.cs.algs4.StdOut;
  5.  
  6. public class MergeNoStaticArray {
  7.  
  8. public static void sort(Comparable[] input) {
  9. int N = input.length;
  10. Comparable[] aux = new Comparable[N];
  11. sort(input, aux, 0, N-1);
  12. }
  13.  
  14. private static void sort(Comparable[] input, Comparable[] aux, int lo, int hi) {
  15.  
  16. if(lo >= hi)//just one entry in array
  17. return;
  18.  
  19. int mid = lo + (hi-lo)/2;
  20. sort(input, aux, lo, mid);
  21. sort(input, aux, mid+1, hi);
  22. merge(input, aux, lo, mid, hi);
  23.  
  24. }
  25.  
  26. private static void merge(Comparable[] input, Comparable[] aux, int lo, int mid, int hi) {
  27.  
  28. //copy input[lo,hi] to aux[lo,hi]
  29. for(int i = lo; i <= hi; i++) {
  30. aux[i] = input[i];
  31. }
  32.  
  33. int i = lo;
  34. int j = mid + 1;
  35. for(int k = lo; k <= hi; k++) {
  36. if(i > mid)
  37. input[k] = aux[j++];
  38. else if(j > hi)
  39. input[k] = aux[i++];
  40. else if(less(aux[j], aux[i]))
  41. input[k] = aux[j++];
  42. else
  43. input[k] = aux[i++];
  44. }
  45.  
  46. StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi);
  47. show(input);//for test
  48.  
  49. }
  50.  
  51. private static boolean less(Comparable v, Comparable w) {
  52.  
  53. return v.compareTo(w) < 0;
  54.  
  55. }
  56.  
  57. private static void show(Comparable[] a) {
  58.  
  59. //print the array, on a single line.
  60. for(int i = 0; i < a.length; i++) {
  61. StdOut.print(a[i] + " ");
  62. }
  63. StdOut.println();
  64.  
  65. }
  66.  
  67. public static boolean isSorted(Comparable[] a) {
  68.  
  69. for(int i = 1; i < a.length; i++) {
  70. if(less(a[i], a[i-1]))
  71. return false;
  72. }
  73.  
  74. return true;
  75.  
  76. }
  77.  
  78. public static void main(String[] args) {
  79.  
  80. //Read strings from standard input, sort them, and print.
  81. String[] input = In.readStrings();
  82. show(input);//for test
  83. sort(input);
  84. assert isSorted(input);
  85. show(input);//for test
  86.  
  87. }
  88.  
  89. }

在merge函数创建aux数组:

  1. package com.qiusongde;
  2.  
  3. import edu.princeton.cs.algs4.In;
  4. import edu.princeton.cs.algs4.StdOut;
  5.  
  6. public class MergeArrayCreation {
  7.  
  8. public static void sort(Comparable[] input) {
  9. int N = input.length;
  10. sort(input, 0, N-1);
  11. }
  12.  
  13. private static void sort(Comparable[] input, int lo, int hi) {
  14.  
  15. if(lo >= hi)//just one entry in array
  16. return;
  17.  
  18. int mid = lo + (hi-lo)/2;
  19. sort(input, lo, mid);
  20. sort(input, mid+1, hi);
  21. merge(input, lo, mid, hi);
  22.  
  23. }
  24.  
  25. private static void merge(Comparable[] input, int lo, int mid, int hi) {
  26.  
  27. //copy input[lo,hi] to aux[lo,hi]
  28. Comparable[] aux = new Comparable[hi-lo+1];
  29. int auxmid = mid - lo;
  30. int auxhi = hi - lo;
  31. for(int i = lo; i <= hi; i++) {
  32. aux[i - lo] = input[i];
  33. }
  34.  
  35. int i = 0;//lo in aux array
  36. int j = (auxmid) + 1;//mid + 1 in aux array
  37. for(int k = lo; k <= hi; k++) {
  38. if(i > auxmid)
  39. input[k] = aux[j++];
  40. else if(j > auxhi)
  41. input[k] = aux[i++];
  42. else if(less(aux[j], aux[i]))
  43. input[k] = aux[j++];
  44. else
  45. input[k] = aux[i++];
  46. }
  47.  
  48. StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi);
  49. show(input);//for test
  50.  
  51. }
  52.  
  53. private static boolean less(Comparable v, Comparable w) {
  54.  
  55. return v.compareTo(w) < 0;
  56.  
  57. }
  58.  
  59. private static void show(Comparable[] a) {
  60.  
  61. //print the array, on a single line.
  62. for(int i = 0; i < a.length; i++) {
  63. StdOut.print(a[i] + " ");
  64. }
  65. StdOut.println();
  66.  
  67. }
  68.  
  69. public static boolean isSorted(Comparable[] a) {
  70.  
  71. for(int i = 1; i < a.length; i++) {
  72. if(less(a[i], a[i-1]))
  73. return false;
  74. }
  75.  
  76. return true;
  77.  
  78. }
  79.  
  80. public static void main(String[] args) {
  81.  
  82. //Read strings from standard input, sort them, and print.
  83. String[] input = In.readStrings();
  84. show(input);//for test
  85. sort(input);
  86. assert isSorted(input);
  87. show(input);//for test
  88.  
  89. }
  90.  
  91. }

性能对比:

  1. package com.qiusongde;
  2.  
  3. import edu.princeton.cs.algs4.StdOut;
  4.  
  5. public class Exercise2226 {
  6.  
  7. public static void main(String[] args) {
  8. String alg1 = "MergeNoStaticArray";
  9. String alg2 = "MergeArrayCreation";
  10. int N = 20000;
  11. int T = 1000;
  12.  
  13. double t1 = SortCompare.timeRandomInput(alg1, N, T);
  14. double t2 = SortCompare.timeRandomInput(alg2, N, T);
  15.  
  16. StdOut.printf("For %d random Doubles %d trials\n", N, T, alg1);
  17. StdOut.printf("%s is %.1fs %s is %.1fs", alg1, t1, alg2, t2);
  18. }
  19.  
  20. }
  1. For 20000 random Doubles 1000 trials
  2. 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. 用yum源安装nginx(转)

    新建一个nginx的源,/etc/yum.repos.d/nginx.repo 编辑此文件内容如下: [nginx]name=nginx repobaseurl=http://nginx.org/pa ...

  2. Spring事务管理之声明式事务管理-基于注解的方式

    © 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(TransactionProxyFactoryBean)管理模拟转账过程 数据库准备 -- 创建表 CREAT ...

  3. Selection Problem (选择问题)

    在一个由n个元素组成的集合中,第i个“顺序统计量(order statistic)”是该集合中第i小的元素.例如,在一个由n个元素组成的集合中,最小值是第1个顺序统计量,最大值是第n个顺序统计量.而“ ...

  4. 下载并导出数据到execl中

    下载poi-3.6-20091214.jar.下载地址例如以下: http://download.csdn.net/detail/evangel_z/3895051 1.jsp <button ...

  5. hive beeline 的server启动与连接

    启动hiveServer2 启动beeline之后 连接 !connect jdbc:hive2://localhost:10000/default 启动的时候连接 /beeline -u jdbc: ...

  6. 【机器学习详解】SMO算法剖析(转载)

    [机器学习详解]SMO算法剖析 转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/51227754 CSDN−勿在浮沙筑高台 本文力 ...

  7. ntp服务及其配置

    集群中使用NTP服务 简介 之前搭建zookeeper时报了一个错,我以为是ntp的问题,结果不是.这里详细学习一下如何在集群中使用ntp服务. 什么是ntp服务 来自ntp的百度百科: NTP服务器 ...

  8. telnet命令的使用

    telnet是啥? Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用tel ...

  9. 中国版Office 365混合部署功能

    中国版Office 365混合部署功能已经正式上线了(原计划6月份推出),虽然支持的类型不如国际版的Office 365全面,但这也标志了该功能与之前相比,已经迈出了重要一步.目前中国版Office ...

  10. android的DrawerLayout用法

    DrawerLayout的关键点(我认为的)就在于layout文件的layout_gravity属性的值,只有左右,两种选择,不能从上下滑出来,就算有这个效果也不是这个套路弄出来的. <?xml ...