算法(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 ...
随机推荐
- PHP接收和发送XML数据(json也通用)
一.接收xml数据, 使用php://input,代码如下: <?php $xmldata=file_get_contents("php://input"); $data=s ...
- Python学习笔记(一)三步走安装pip
pip是用来方便地管理Python的第三方包的,由于此前玩Python仅仅是浅尝辄止,用的是python(x,y),但是这里并不代表你想用什么包都能从里面找到的,所以我把python(x,y)卸了,然 ...
- OpenLayers加载天地图
openlayer 是基于JavaScript的webGIS库 ,通过openlayer可以很容易的调用地图,并做相应的操作. 在head中载入openlayer的js文件: <link rel ...
- android学习笔记(3)Button控件的学习
一,增加一个button并用外部类绑定事件 //XML文件: <Button android:id="@+id/button1" android:layout_width=& ...
- ifconfig 命令
许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...
- 搭建属于你的家庭网络实时监控–HTML5在嵌入式系统中的应用·高级篇
*本文已刊登在<无线电>2014年第6期 <搭建属于你的在线实时採集系统>中已经对HTML5平台有了初步的认识,并基于此向大家展示了怎样将採集到的数据上传至网络.实现实时观測. ...
- 谷歌宣称web组件才是web开发的未来
谷歌宣称web组件才是web开发的未来 虽然今年的谷歌I/O大会没有出现像去年谷歌眼镜发布时直播疯狂跳伞这样的活动,但是上周仍然有不少产品推出.谷歌宣布对谷歌地图.搜索.安卓,以及其他 很多产品做出更 ...
- 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用
和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...
- 四边形不等式优化DP——石子合并问题 学习笔记
好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...
- Linux下文件的基本操作
文件的基本操作 新建和删除文件夹 命令#mkdir /file 在当前目录创建file文件夹 命令#rmdir /file 删除当前目录下file文件夹 复制和移动文件 命令#cp text/file ...