当前位置: 首页 > news >正文

算法(Algorithms)第4版 练习 2.3.25

 

代码实现:

  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

 

转载于:https://www.cnblogs.com/songdechiu/p/6635445.html

相关文章:

  • Matlab中imnoise函数的用法
  • docker 配置缓存代理服务apt-cacher-ng
  • TCP状态统计 - 脚本命令
  • Flask的Jinja2模板引擎 - 全局函数
  • 二叉树性质
  • Win10系列:C#应用控件基础2
  • Ubuntu下搭建tftp服务器最简单方法
  • PHP基础笔记【2】字符串操作
  • 深入分析Java单例模式的各种方案
  • 左手书法二十七篇
  • Flink - NetworkEnvironment
  • 修改Jmeter配置使能支持更大并发
  • 关于grep正则表达式-1
  • web前端性能优化总结
  • Cloudera Manager是啥?主要是干啥的?
  • 30天自制操作系统-2
  • IDEA 插件开发入门教程
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • Mithril.js 入门介绍
  • Node + FFmpeg 实现Canvas动画导出视频
  • node 版本过低
  • PHP的Ev教程三(Periodic watcher)
  • spring security oauth2 password授权模式
  • SSH 免密登录
  • Swift 中的尾递归和蹦床
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • XForms - 更强大的Form
  • 包装类对象
  • 回顾 Swift 多平台移植进度 #2
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 聊聊sentinel的DegradeSlot
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 面试总结JavaScript篇
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 山寨一个 Promise
  • 收藏好这篇,别再只说“数据劫持”了
  • 思否第一天
  • 我与Jetbrains的这些年
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 在weex里面使用chart图表
  • 栈实现走出迷宫(C++)
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • Android开发者必备:推荐一款助力开发的开源APP
  • ​香农与信息论三大定律
  • #Linux(make工具和makefile文件以及makefile语法)
  • #微信小程序(布局、渲染层基础知识)
  • $NOIp2018$劝退记
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (9)目标检测_SSD的原理
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (九)信息融合方式简介
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF