算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化
一、介绍
1.
2.
二、代码
1.
package algorithms.elementary21; /******************************************************************************
* Compilation: javac SortCompare.java
* Execution: java SortCompare alg1 alg2 N T
* Dependencies: StdOut.java Stopwatch.java
*
* Sort N random real numbers, T times using the two
* algorithms specified on the command line.
*
* % java SortCompare Insertion Selection 1000 100
* For 1000 random Doubles
* Insertion is 1.7 times faster than Selection
*
* Note: this program is designed to compare two sorting algorithms with
* roughly the same order of growth, e,g., insertion sort vs. selection
* sort or mergesort vs. quicksort. Otherwise, various system effects
* (such as just-in-time compiliation) may have a significant effect.
* One alternative is to execute with "java -Xint", which forces the JVM
* to use interpreted execution mode only.
*
******************************************************************************/ import java.util.Arrays; import algorithms.analysis14.Stopwatch;
import algorithms.util.StdOut;
import algorithms.util.StdRandom; public class SortCompare { public static double time(String alg, Double[] a) {
Stopwatch sw = new Stopwatch();
if (alg.equals("Insertion")) Insertion.sort(a);
else if (alg.equals("InsertionX")) InsertionX.sort(a);
else if (alg.equals("BinaryInsertion")) BinaryInsertion.sort(a);
else if (alg.equals("Selection")) Selection.sort(a);
else if (alg.equals("Bubble")) Bubble.sort(a);
else if (alg.equals("Shell")) Shell.sort(a);
else if (alg.equals("Merge")) Merge.sort(a);
else if (alg.equals("MergeX")) MergeX.sort(a);
else if (alg.equals("MergeBU")) MergeBU.sort(a);
else if (alg.equals("Quick")) Quick.sort(a);
else if (alg.equals("Quick3way")) Quick3way.sort(a);
else if (alg.equals("QuickX")) QuickX.sort(a);
else if (alg.equals("Heap")) Heap.sort(a);
else if (alg.equals("System")) Arrays.sort(a);
else throw new IllegalArgumentException("Invalid algorithm: " + alg);
return sw.elapsedTime();
} // Use alg to sort T random arrays of length N.
public static double timeRandomInput(String alg, int N, int T) {
double total = 0.0;
Double[] a = new Double[N];
// Perform one experiment (generate and sort an array).
for (int t = 0; t < T; t++) {
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
total += time(alg, a);
}
return total;
} // Use alg to sort T random arrays of length N.
public static double timeSortedInput(String alg, int N, int T) {
double total = 0.0;
Double[] a = new Double[N];
// Perform one experiment (generate and sort an array).
for (int t = 0; t < T; t++) {
for (int i = 0; i < N; i++)
a[i] = 1.0 * i;
total += time(alg, a);
}
return total;
} public static void main(String[] args) {
String alg1 = args[0];
String alg2 = args[1];
int N = Integer.parseInt(args[2]);
int T = Integer.parseInt(args[3]);
double time1, time2;
if (args.length == 5 && args[4].equals("sorted")) {
time1 = timeSortedInput(alg1, N, T); // Total for alg1.
time2 = timeSortedInput(alg2, N, T); // Total for alg2.
}
else {
time1 = timeRandomInput(alg1, N, T); // Total for alg1.
time2 = timeRandomInput(alg2, N, T); // Total for alg2.
} StdOut.printf("For %d random Doubles\n %s is", N, alg1);
StdOut.printf(" %.1f times faster than %s\n", time2/time1, alg2);
}
}
2.
package algorithms.elementary21; import algorithms.util.StdDraw;
import algorithms.util.StdRandom; /******************************************************************************
* Compilation: javac InsertionBars.java
* Execution: java InsertionBars N
* Dependencies: StdDraw.java
*
* Insertion sort N random real numbers between 0 and 1, visualizing
* the results by ploting bars with heights proportional to the values.
*
* % java InsertionBars 20
*
******************************************************************************/ public class InsertionBars {
public static void sort(double[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int j = i;
while (j >= 1 && less(a[j], a[j-1])) {
exch(a, j, j-1);
j--;
}
show(a, i, j);
}
} private static void show(double[] a, int i, int j) {
StdDraw.setYscale(-a.length + i + 1, i);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = 0; k < j; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.line(j, 0, j, a[j]*.6);
StdDraw.setPenColor(StdDraw.BLACK);
for (int k = j+1; k <= i; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = i+1; k < a.length; k++)
StdDraw.line(k, 0, k, a[k]*.6);
} private static boolean less(double v, double w) {
return v < w;
} private static void exch(double[] a, int i, int j) {
double t = a[i];
a[i] = a[j];
a[j] = t;
} public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
StdDraw.setCanvasSize(160, 640);
StdDraw.setXscale(-1, N+1);
StdDraw.setPenRadius(.006);
double[] a = new double[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
sort(a);
} }
3.
package algorithms.elementary21; import algorithms.util.StdDraw;
import algorithms.util.StdRandom; /******************************************************************************
* Compilation: javac SelectionBars.java
* Execution: java SelectionBars N
* Dependencies: StdDraw.java
*
* Selection sort N random real numbers between 0 and 1, visualizing
* the results by ploting bars with heights proportional to the values.
*
* % java SelectionBars 20
*
******************************************************************************/ public class SelectionBars { public static void sort(double[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int min = i;
for (int j = i+1; j < N; j++)
if (less(a[j], a[min])) min = j;
show(a, i, min);
exch(a, i, min);
}
} private static void show(double[] a, int i, int min) {
StdDraw.setYscale(-a.length + i + 1, i);
StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
for (int k = 0; k < i; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.BLACK);
for (int k = i; k < a.length; k++)
StdDraw.line(k, 0, k, a[k]*.6);
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.line(min, 0, min, a[min]*.6);
} private static boolean less(double v, double w) {
return v < w;
} private static void exch(double[] a, int i, int j) {
double t = a[i];
a[i] = a[j];
a[j] = t;
} public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
StdDraw.setCanvasSize(260, 640);
StdDraw.setXscale(-1, N+1);
StdDraw.setPenRadius(.006);
double[] a = new double[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform();
sort(a);
} }
4.
算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化的更多相关文章
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-008排序算法的复杂度(比较次数的上下限)
一. 1. 2.
随机推荐
- Linux运维工程师中级面试题
1.解释top命令和vmstat命令 2.请写出iptables语句 3.mysql高可用方案有哪些?mysql备份方案有哪些?有什么缺点? 4.写出Apache 2.x的两种工作模式,以及各自的工作 ...
- js 去除字符串两边的空格
js 去除字符串两边的空格 function trim(str){ //删除左右两端的空格 return str.replace(/(^\s*)|(\s*$)/g, "&q ...
- Effective Python之编写高质量Python代码的59个有效方法
这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...
- sklearn_算法选择
- Python 中的几种矩阵乘法 np.dot, np.multiply, *
使用array时,运算符 * 用于计算数量积(点乘),函数 dot() 用于计算矢量积(叉乘).使用matrix时,运算符 * 用于计算矢量积,函数 multiply() 用于计算数量积. 下面是使用 ...
- Redis底层探秘(五):Redis对象
前面几篇文章,我们一起学习了redis用到的所有主要数据结构,比如简单动态字符串(sds).双端链表.字典.压缩列表.整数集合等等. redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这 ...
- 用反射封装HttpHandler,实现通过action方法名调用方法
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案
错误: 在Springboot 框架中使用JPA的过程中,怎么来实现数据库操作底层的交互呢?Spring JPA其实已经提供了一套很全面的解决方案,实现对数据库的增.删.查.改只需要继承JPA实现类 ...
- Servlet3.0之八:基于Servlet3.0的文件上传@MultipartConfig
在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ...
- Sass、Less和Stylus
1.背景介绍 1.Sass背景介绍 Sass是对CSS(层叠样式表)的语法的一种扩充,诞生于2007年,最早也是最成熟的一款CSS预处理器语言,它可以使用变量.常量.嵌套.混 入.函数等功能,可以更有 ...