算法(Algorithms)第4版 练习 2.2.26
在sort函数创建aux数组:
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeNoStaticArray { public static void sort(Comparable[] input) {
int N = input.length;
Comparable[] aux = new Comparable[N];
sort(input, aux, 0, N-1);
} private static void sort(Comparable[] input, Comparable[] aux, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(input, aux, lo, mid);
sort(input, aux, mid+1, hi);
merge(input, aux, lo, mid, hi); } private static void merge(Comparable[] input, Comparable[] aux, 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 } }
在merge函数创建aux数组:
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class MergeArrayCreation { public static void sort(Comparable[] input) {
int N = input.length;
sort(input, 0, N-1);
} private static void sort(Comparable[] input, int lo, int hi) { if(lo >= hi)//just one entry in array
return; int mid = lo + (hi-lo)/2;
sort(input, lo, mid);
sort(input, mid+1, hi);
merge(input, lo, mid, hi); } private static void merge(Comparable[] input, int lo, int mid, int hi) { //copy input[lo,hi] to aux[lo,hi]
Comparable[] aux = new Comparable[hi-lo+1];
int auxmid = mid - lo;
int auxhi = hi - lo;
for(int i = lo; i <= hi; i++) {
aux[i - lo] = input[i];
} int i = 0;//lo in aux array
int j = (auxmid) + 1;//mid + 1 in aux array
for(int k = lo; k <= hi; k++) {
if(i > auxmid)
input[k] = aux[j++];
else if(j > auxhi)
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 } }
性能对比:
package com.qiusongde;
import edu.princeton.cs.algs4.StdOut;
public class Exercise2226 {
public static void main(String[] args) {
String alg1 = "MergeNoStaticArray";
String alg2 = "MergeArrayCreation";
int N = 20000;
int T = 1000;
double t1 = SortCompare.timeRandomInput(alg1, N, T);
double t2 = SortCompare.timeRandomInput(alg2, N, T);
StdOut.printf("For %d random Doubles %d trials\n", N, T, alg1);
StdOut.printf("%s is %.1fs %s is %.1fs", alg1, t1, alg2, t2);
}
}
For 20000 random Doubles 1000 trials
MergeNoStaticArray is 3.3s MergeArrayCreation is 3.9s
算法(Algorithms)第4版 练习 2.2.26的更多相关文章
- 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下yum安装maven
Maven 官网:http://maven.apache.org/ 源码安装 http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binarie ...
- ACM算法整理(不断补充ing)
动态规划 1.背包问题 (1)01背包 ,n) DFR(v,V,C[i]) F[v]=max(F[v],F[v-C[i]]+W[i]); } //初始化时 //若背包不一定装满F全初始化为0 //若装 ...
- web service json 数组解析
boolean workexpMark = true; // 美发师工作经历json数组解析 org.json.JSONObject jsonObject = new org.j ...
- ORACLE 12C R2 RAC 安装配置指南
>> from zhuhaiqing.info ASM磁盘空间最低要求 求12C R2相比前一版本,OCR的磁盘占用需求有了明显增长.为了方便操作,设置如下:External: 1个卷x4 ...
- bbb u-boot SPI 启动
beagle bone black的u-boot编译时已经为SPI准备好了 MLO.byteswap,这个文件应该直接写入到SPI flash的偏移0位置,根据am335x的手册,SPI内可以保存多份 ...
- [译]GLUT教程 - 笔划字体
Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Stroke Fonts 笔划字体是用线条生成的.跟位图字体相反,笔划字 ...
- ifconfig 命令
许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...
- 内核initcall分析
linux中init相关内容定义在include/linux/init.h initcall相关定义 先看下文件说明,此文件定义的宏主要用于初始化阶段标记函数或初始化数据,之后占用的资源会被释放掉. ...
- 第二篇:Filebeat 安装配置
Filebeat 简介:Filebeat 是一款轻量型日志收集工具,可转发汇总日志.文件等内容. 其主要特点为:1. 断点续传.(如遇日志转发过程中网络 ...
- ubuntu防火墙 ufw配置
https://www.cnblogs.com/ylan2009/articles/2321136.html