算法(Algorithms)第4版 练习 2.3.17
关键代码:
public static void sort(Comparable[] a) {
StdRandom.shuffle(a);//eliminate dependence on input
StdOut.print("After shuffle:");//for test
show(a);//for test
int largest = largest(a);
exch(a, largest, a.length - 1);
StdOut.print("After find largest and exchange:");//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)
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
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;
}
测试用例:
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom; public class QuickSentinel { public static void sort(Comparable[] a) {
StdRandom.shuffle(a);//eliminate dependence on input
StdOut.print("After shuffle:");//for test
show(a);//for test
int largest = largest(a);//find largest item
exch(a, largest, a.length - 1);//set sentinel in right bound
StdOut.print("After find largest and exchange:");//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)
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
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 int largest(Comparable[] a) {
assert a != null;
assert a.length > 1; int largestindex = 0;
for(int i = 1; i < a.length; i++) {
if(less(a[largestindex], a[i]))
largestindex = i;
} return largestindex;
} 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);
sort(a);
assert isSorted(a);
show(a);
} }
输出结果:
红色部分可看到右子数组的最左元素可作为左子数组右边界的哨兵。
K R A T E L E P U I M Q C X O S
After shuffle:E S I X E T P K C O R Q L U M A
After find largest and exchange:E S I A E T P K C O R Q L U M X partition(input, 0, 15)
i:1 j:8
E C I A E T P K S O R Q L U M X
i:2 j:4
E C E A I T P K S O R Q L U M X
i:4 j:3
j is 3
A C E E I T P K S O R Q L U M X partition(input, 0, 2)
i:1 j:0
j is 0
A C E E I T P K S O R Q L U M X partition(input, 1, 2)
i:2 j:1
j is 1
A C E E I T P K S O R Q L U M X partition(input, 4, 15)
i:5 j:4
j is 4
A C E E I T P K S O R Q L U M X partition(input, 5, 15)
i:13 j:14
A C E E I T P K S O R Q L M U X
i:14 j:13
j is 13
A C E E I M P K S O R Q L T U X partition(input, 5, 12)
i:6 j:12
A C E E I M L K S O R Q P T U X
i:8 j:7
j is 7
A C E E I K L M S O R Q P T U X partition(input, 5, 6)
i:6 j:5
j is 5
A C E E I K L M S O R Q P T U X partition(input, 8, 12)
i:13 j:12
j is 12
A C E E I K L M P O R Q S T U X partition(input, 8, 11)
i:10 j:9
j is 9
A C E E I K L M O P R Q S T U X partition(input, 10, 11)
i:12 j:11
j is 11
A C E E I K L M O P Q R S T U X partition(input, 14, 15)
i:15 j:14
j is 14
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
性能对比:
For 2000 random Doubles 10000 trials
Quick is 2.3s QuickSentinel is 2.3s
没有大区别。
算法(Algorithms)第4版 练习 2.3.17的更多相关文章
- 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 ...
随机推荐
- mysql中去重复记录
Distinct 这个只能放在查询语句的最前面 参考 : https://www.cnblogs.com/lushilin/p/6187743.html
- Windows安装Redis的php扩展
Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不 ...
- python easy install时,使用aliyun阿里云镜像提示主机名不匹配的问题
因网络问题,因此设置 easy_install 使用阿里云的源, ## 更新 easy_install 源 tee ~/.pydistutils.cfg <<-'EOF' [easy_in ...
- Git(四):理解和使用分支
分支是Git的核心内容之中的一个,本章将介绍分支的一些知识,这里将继续使用前面创建的版本号库. 假设你跳过了前面章节直接进入本章.能够从Github上克隆这个版本号库: $ git clon ...
- 【Python学习】之yagmail库实现发送邮件
上代码: import yagmail sendmail = 'xxx@126.com' sendpswd = 'xxx' receivemail = 'xxx@qq.com' # 连接邮箱服务器 y ...
- 12 Memcached 缓存无底洞现象
一:Memcached 缓存无底洞现象(1)facebook的工作人员反应的,facebook在2010年左右,memcached节点就已经达到了3000个,存储的数据进千G的数据存储. 他们发现一个 ...
- Python 类方法、实例方法、静态方法
实例方法:类中第一个参数为self的方法. 类方法:类中第一个参数为类,约定写为cls,并被@classmethod修饰的方法. 静态方法:类中被@staticmethod修饰的方法. 类变量:定义在 ...
- 图论——Dijkstra+prim算法涉及到的优先队列(二叉堆)
[0]README 0.1)为什么有这篇文章?因为 Dijkstra算法的优先队列实现 涉及到了一种新的数据结构,即优先队列(二叉堆)的操作需要更改以适应这种新的数据结构,我们暂且吧它定义为Dista ...
- IOS启动页动画(uiview 淡入淡出效果 )2
Appdelegate里面右个这个函数,只要它没结束,你的等待界面就不会消失.以在启动的时候做些动画 - (BOOL)application:(UIApplication *)application ...
- hdu 2036 改革春风吹满地【求多边形面积模板】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2036 http://acm.hust.edu.cn/vjudge/contest/view.action ...