代码实现:

  public static void sort(Comparable[] a) {
StdRandom.shuffle(a);//eliminate dependence on input
StdOut.print("After shuffle:");//for test
show(a);//for test
sort(a, 0, a.length-1);
} private static void sort(Comparable[] a, int lo, int hi) { if(hi <= lo + CUTOFF) {
if(hi > lo)
Insertion.sort(a, lo, hi);
return;
} int j = partition(a, lo, hi);
sort(a, lo, j-1);
sort(a, j+1, hi); }

单元测试:

package com.qiusongde;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom; public class QuickCutoffInsertion { private static int CUTOFF = 4;//default value is 8 public static void setCutoff(int cutoff) {
assert cutoff > 0;
CUTOFF = cutoff;
} public static void sort(Comparable[] a) {
StdRandom.shuffle(a);//eliminate dependence on input
StdOut.print("After shuffle:");//for test
show(a);//for test
sort(a, 0, a.length-1);
} private static void sort(Comparable[] a, int lo, int hi) { if(hi <= lo + CUTOFF) {
if(hi > lo)
Insertion.sort(a, lo, hi);
return;
} int j = partition(a, lo, hi);
sort(a, lo, j-1);
sort(a, j+1, hi); } private static int partition(Comparable[] a, int lo, int hi) { int i = lo;
int j = hi + 1;
Comparable v = a[lo]; StdOut.println();//for test
StdOut.printf("partition(input, %4d, %4d)\n", lo, hi);//for test while(true) {
while(less(a[++i], v))//find item larger or equal to v
if(i == hi)
break;
while(less(v, a[--j]));//not need to worry about j will be out of bound StdOut.println("i:" + i + " j:" + j);//for test if(i >= j)//cross
break; exch(a, i, j);
show(a);//for test
}
exch(a, lo, j); StdOut.printf("j is %4d\n", j);//for test
show(a);//for test return j; } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i];
a[i] = a[j];
a[j] = t; } 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[] a = In.readStrings();
show(a);//for test
sort(a);
assert isSorted(a);
show(a);//for test
} }

输出结果:

K R A T E L E P U I M Q C X O S
After shuffle:E R K U M C X A I O E S T P Q L partition(input, 0, 15)
i:1 j:10
E E K U M C X A I O R S T P Q L
i:2 j:7
E E A U M C X K I O R S T P Q L
i:3 j:5
E E A C M U X K I O R S T P Q L
i:4 j:3
j is 3
C E A E M U X K I O R S T P Q L Insertion.sort(input, 0, 2)
A C E E M U X K I O R S T P Q L partition(input, 4, 15)
i:5 j:15
A C E E M L X K I O R S T P Q U
i:6 j:8
A C E E M L I K X O R S T P Q U
i:8 j:7
j is 7
A C E E K L I M X O R S T P Q U Insertion.sort(input, 4, 6)
A C E E I K L M X O R S T P Q U partition(input, 8, 15)
i:15 j:15
j is 15
A C E E I K L M U O R S T P Q X partition(input, 8, 14)
i:14 j:14
j is 14
A C E E I K L M Q O R S T P U X partition(input, 8, 13)
i:10 j:13
A C E E I K L M Q O P S T R U X
i:11 j:10
j is 10
A C E E I K L M P O Q S T R U X Insertion.sort(input, 8, 9)
A C E E I K L M O P Q S T R U X Insertion.sort(input, 11, 13)
A C E E I K L M O P Q R S T U X
A C E E I K L M O P Q R S T U X

性能测试:

package com.qiusongde;

import edu.princeton.cs.algs4.StdOut;

public class Exercise2325 {

    public static void main(String[] args) {

        String alg = "QuickCutoffInsertion";

        int T = 10;//T trials

        for(int N = 1000; N <= 1000000; N *= 10){
StdOut.println("N:" + N);
for(int M = 0; M <= 30; M++) {
QuickCutoffInsertion.setCutoff(M);
double time = SortCompare.timeRandomInput(alg, N, T);
StdOut.printf("%8.4f", time);
}
StdOut.println();
} } }

输出结果:

N:1000
0.0050 0.0030 0.0060 0.0060 0.0060 0.0060 0.0060 0.0030 0.0010 0.0010 0.0030 0.0010 0.0020 0.0010 0.0020 0.0010 0.0000 0.0010 0.0010 0.0000 0.0020 0.0000 0.0020 0.0010 0.0020 0.0000 0.0020 0.0020 0.0000 0.0020 0.0000
N:10000
0.0200 0.0180 0.0140 0.0140 0.0130 0.0130 0.0140 0.0130 0.0120 0.0150 0.0130 0.0130 0.0100 0.0150 0.0120 0.0130 0.0130 0.0110 0.0120 0.0130 0.0150 0.0110 0.0120 0.0120 0.0150 0.0130 0.0130 0.0140 0.0140 0.0110 0.0150
N:100000
0.2070 0.2070 0.2010 0.1960 0.1970 0.1970 0.2040 0.2010 0.1800 0.1820 0.1820 0.1780 0.1790 0.1820 0.1800 0.1810 0.1850 0.1860 0.1750 0.1810 0.1810 0.1820 0.1790 0.1830 0.1810 0.1820 0.1840 0.1830 0.1830 0.1840 0.1830
N:1000000
3.9260 3.9100 3.9300 3.9050 3.8640 3.8250 3.7930 3.8780 3.8090 3.8360 3.7860 3.8530 3.8130 3.7770 3.7800 3.7570 3.8220 3.7800 3.8220 3.7850 3.7970 3.8020 3.8530 3.8340 3.8710 3.7660 3.8080 3.8580 3.8350 3.8890 3.7810

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

  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. ubuntu16.04 安装系统之后的开发必备-sourcelist--idk-sublime--opencv

    设置sourcelist.txt # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ub ...

  2. 在Ubuntu 16.04下安装 virtualbox 5.2

        sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian xenial contrib" ...

  3. smarty模版使用php标签,如何获取模版变量

    smarty模版使用php标签,如何获取模版变量 in: 后端程序 已经assign一个模版变量$assign,由于要做特殊的循环输出,使用for循环,因此使用到了php标签,但是php语句和模版语句 ...

  4. php给图片添加文字水印方法汇总

    在php中要给图片加水印我们需要给php安装GD库了,这里我们不介绍GD库安装,只介绍怎么利用php给图片添加文字水印的4种方法的汇总.有需要的小伙伴可以参考下. 1: 面向过程的编写方法 1 2 3 ...

  5. C# 中安全代码与不安全代码

    C# 中安全代码与不安全代码 P/Invoke 非托管代码需要在unsafe块中书写. using System; using System.Collections.Generic; using Sy ...

  6. element-ui table 点击分页table滚动到顶部

    在做项目中,碰到一个问题,table加了固定头,内容可滚动,当滚到table底边时,点击分页后还在底边 解决方法:设置table的 ref='multipleTable' //切换分页的方法加上下面这 ...

  7. Eclipse下使用maven搭建多模块项目

    暂时将项目分为如下几层: domain(域模型层).dao(数据库访问层).service(业务逻辑层).web(表现层),有需要再另行添加(如common等): 目录结构: 一.app 该层为父层, ...

  8. ubuntu 安装wine

    笔记 1.安装源 sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update 2.安装wine sudo apt-get inst ...

  9. 优化tomcat启动速度

    1.去掉不需要的jar包,这样tomcat在启动时就可以少加载jar包里面的class文件. 2.跳过一些与TLD files.注解.网络碎片无关的jar包,通过在conf/catalina.prop ...

  10. iOS解决导航引起视图高度问题

    经过导航栏跨越的坑,总结出有两种方法可以无痕解决(前提>=iOS7版本)(TabBar与导航栏类似) 1.通过设置导航栏的透明度实现(这种方式的控制器view的起始坐标是充(0,64)开始的) ...