算法(Algorithms)第4版 练习 2.2.11(1)
实现关键代码:
private static void sort(Comparable[] input, int lo, int hi) {
if((lo+CUTOFF-1) >= hi) { //use insertion sort for tiny subarrays
insertionsort(input, lo, hi);
return;
}
int mid = lo + (hi-lo)/2;
sort(input, lo, mid);
sort(input, mid+1, hi);
merge(input, lo, mid, hi);
}
整体:
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeUseInsert { private static Comparable[] aux;
private final static int CUTOFF = 8;//size public static void sort(Comparable[] input) {
int N = input.length;
aux = new Comparable[N];
sort(input, 0, N-1);
} private static void sort(Comparable[] input, int lo, int hi) { if((lo+CUTOFF-1) >= hi) { //use insertion sort for tiny subarrays
insertionsort(input, lo, hi);
return;
} int mid = lo + (hi-lo)/2;
sort(input, lo, mid);
sort(input, mid+1, hi);
merge(input, lo, mid, hi); } private static void insertionsort(Comparable[] input, int lo, int hi) {
for(int i = lo + 1; i <= hi; i++) {
for(int j = i; j > lo && less(input[j], input[j-1]); j--) {
exch(input, j, j-1);
}
} StdOut.printf("insertionsort(input, %4d, %4d)", lo, hi);//for test
show(input);//for test
} private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i];
a[i] = a[j];
a[j] = t; } private static void merge(Comparable[] input, 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 } }
验证:
M E R G E S O R T E X A M P L E
insertionsort(input, 0, 7)E E G M O R R S T E X A M P L E
insertionsort(input, 8, 15)E E G M O R R S A E E L M P T X
merge(input, 0, 7, 15)A E E E E G L M M O P R R S T X
A E E E E G L M M O P R R S T X
性能对比:
For 20000 random Doubles 1000 trials
Merge is 3.4s MergeFasterM is 3.1s MergeUseInsert is 3.0s
算法(Algorithms)第4版 练习 2.2.11(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 ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- 在Linux中配置DNS的正确方式
链接:http://ccl.cse.nd.edu/operations/condor/hostname.shtml Common Hostname Problem on Linux Newly ins ...
- Foundation框架 - NSDictionary类、NSMutableDictionary类
NSArray.NSSet.NSDictionary /* 集合 1.NSArray\NSMutableArray * 有序 * 高速创建(不可变):@[obj1, obj2, obj3] * 高速訪 ...
- homebrew可以管理众多开源软件的安装和卸载
通过homebrew可以管理众多开源软件的安装和卸载. 参考https://github.com/mxcl/homebrew/wiki 1. 安装: ruby -e "$(curl -fsS ...
- justify-content 定义子元素在父元素水平位置排列的顺序
justify-content 定义子元素在父元素水平位置排列的顺序,需要和display:flex使用才会生效. 有五个属性: 1.flex-start(默认值) 左对齐 2.flex-end 右 ...
- phpcms控制器变量分配到模板
跟TP.CI框架不同,phpcmsv9分配变量的方式是: 控制器中声明了变量$a='zrp'或$data=array('aa','bb'); 在模板中就可以直接输出: 字符串:{$a} 数组:遍历 { ...
- android 自定义 listView
目录: 1.主布局 ListView <?xml version="1.0" encoding="utf-8"?><RelativeLayou ...
- windows监控 排查蓝屏问题
DUMP包分析 工具WinDbg,下载打开,按ctrl+S 输入:SRV*C:\websymbols*http://msdl.microsoft.com/download/symbols:点击OK, ...
- EasyNVR无插件直播服务器软件使用详情功能 - 录像功能说明
背景介绍 EasyNVR不仅仅拥有无插件的直播功能,更拥有对于直播录像的存储和日期检索功能: 本篇博文主要用于介绍EasyNVR的录像功能. 之前有博文介绍相关的录像功能,本篇主要为了介绍录像的新功能 ...
- iOS 邓白氏编码的申请
http://www.cocoachina.com/ios/20161214/18225.html
- cocos2dx使用cocostudio导出的scene
local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...