算法(Algorithms)第4版 练习 2.1.25
2024-08-27 10:47:53
代码实现:
package com.qiusongde; import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class InsertionHalfExchange { public static void sort(Comparable[] a) { int N = a.length; for(int i = 1; i < N; i++) {
Comparable temp = a[i];
int j = i;
while(j > 0 && less(temp, a[j-1])) {
a[j] = a[j-1];
j--;
}
a[j] = temp;
// show(a);
} } private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i];
a[i] = a[j];
a[j] = t; } 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);
} }
单元测试结果:
S O R T E X A M P L E
O S R T E X A M P L E
O R S T E X A M P L E
O R S T E X A M P L E
E O R S T X A M P L E
E O R S T X A M P L E
A E O R S T X M P L E
A E M O R S T X P L E
A E M O P R S T X L E
A E L M O P R S T X E
A E E L M O P R S T X
A E E L M O P R S T X
比较Insertion和InsertionHalfExchange 的性能:
package com.qiusongde; import edu.princeton.cs.algs4.Shell;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom; public class SortCompare { public static double timeRandomInput(String alg, int N, int T) { double total = 0.0; Double[] a = new Double[N];
for(int t = 0; t < T; t++) {
for(int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
total += time(alg, a);
} return total; } public static double time(String alg, Double[] a) { Stopwatch timer = new Stopwatch(); if(alg.equals("Selection"))
Selection.sort(a);
if(alg.equals("Insertion"))
Insertion.sort(a);
if(alg.equals("InsertionHalfExchange"))
InsertionHalfExchange.sort(a);
if(alg.equals("Shell"))
Shell.sort(a); return timer.elapsedTime(); } public static void main(String[] args) { String alg1 = "Insertion";
String alg2 = "InsertionHalfExchange";
int N = 1000;
int T = 1000; double t1 = timeRandomInput(alg1, N, T);
double t2 = 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 1000 random Doubles 1000 trials
Insertion is 1.1s InsertionHalfExchange is 0.5s
最新文章
- 用 nssm 把 Nginx 安装成 Windows 服务方法
- iOS instruments trace文件解析方案
- HTML Inspector – 帮助你编写高质量的 HTML 代码
- Requests库上传文件时UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte错误解析
- UVA294DIvisors(唯一分解定理+约数个数)
- 抛弃优启Grub4dos和PE大多数时间可以这样用
- Android TouchEvent事件传递机制
- IIS发布,图片和样式显示不了的问题
- Java API —— IO流( FileInputStream &; FileOutputStream &; BufferedInputStream &; BufferedOutputStream )
- [Silverlight]常见问题
- servlet的跳转
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- 查找maven中的groupId,artifactId,version等信息的方式
- Omi框架学习之旅 - 组件 及原理说明
- laravel使用过程中一些总结
- pat树之专题(30分)
- Express入门介绍vs实例讲解
- 集群扩容的常规解决:一致性hash算法
- java TreeNode接口
- python 获取命令行输出结果
热门文章
- Spring事务管理之编程式事务管理
- Android环境变量的设置(详细图解版)
- 网络启动并安装Debian
- MyBatis随笔
- eclipse maven 依赖jar下载失败解决办法
- [转]maven2中snapshot快照库和release发布库的应用
- vue-router实现页面的整体跳转
- C​#​获​取​当​前​时​间​的​各​种​格​式
- 使用3DES+Base64来加密传输iOS应用数据
- JS获取图片的缩略图,并且动态的加载多张图片