实现关键代码:

  1. private static void sort(Comparable[] input, int lo, int hi) {
  2.  
  3. if((lo+CUTOFF-1) >= hi) { //use insertion sort for tiny subarrays
  4. insertionsort(input, lo, hi);
  5. return;
  6. }
  7.  
  8. int mid = lo + (hi-lo)/2;
  9. sort(input, lo, mid);
  10. sort(input, mid+1, hi);
  11. merge(input, lo, mid, hi);
  12.  
  13. }

整体:

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

验证:

  1. M E R G E S O R T E X A M P L E
  2. insertionsort(input, 0, 7)E E G M O R R S T E X A M P L E
  3. insertionsort(input, 8, 15)E E G M O R R S A E E L M P T X
  4. merge(input, 0, 7, 15)A E E E E G L M M O P R R S T X
  5. A E E E E G L M M O P R R S T X

性能对比:

  1. For 20000 random Doubles 1000 trials
  2. Merge is 3.4s MergeFasterM is 3.1s MergeUseInsert is 3.0s

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

  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接收和发送XML数据(json也通用)

    一.接收xml数据, 使用php://input,代码如下: <?php $xmldata=file_get_contents("php://input"); $data=s ...

  2. Python学习笔记(一)三步走安装pip

    pip是用来方便地管理Python的第三方包的,由于此前玩Python仅仅是浅尝辄止,用的是python(x,y),但是这里并不代表你想用什么包都能从里面找到的,所以我把python(x,y)卸了,然 ...

  3. OpenLayers加载天地图

    openlayer 是基于JavaScript的webGIS库 ,通过openlayer可以很容易的调用地图,并做相应的操作. 在head中载入openlayer的js文件: <link rel ...

  4. android学习笔记(3)Button控件的学习

    一,增加一个button并用外部类绑定事件 //XML文件: <Button android:id="@+id/button1" android:layout_width=& ...

  5. ifconfig 命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  6. 搭建属于你的家庭网络实时监控–HTML5在嵌入式系统中的应用&#183;高级篇

    *本文已刊登在<无线电>2014年第6期 <搭建属于你的在线实时採集系统>中已经对HTML5平台有了初步的认识,并基于此向大家展示了怎样将採集到的数据上传至网络.实现实时观測. ...

  7. 谷歌宣称web组件才是web开发的未来

    谷歌宣称web组件才是web开发的未来 虽然今年的谷歌I/O大会没有出现像去年谷歌眼镜发布时直播疯狂跳伞这样的活动,但是上周仍然有不少产品推出.谷歌宣布对谷歌地图.搜索.安卓,以及其他 很多产品做出更 ...

  8. 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用

    和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...

  9. 四边形不等式优化DP——石子合并问题 学习笔记

    好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...

  10. Linux下文件的基本操作

    文件的基本操作 新建和删除文件夹 命令#mkdir /file 在当前目录创建file文件夹 命令#rmdir /file 删除当前目录下file文件夹 复制和移动文件 命令#cp text/file ...