Generally we compare algorithms by
■ Implementing and debugging them
■ Analyzing their basic properties
■ Formulating a hypothesis about comparative performance
■ Running experiments to validate the hypothesis
These steps are nothing more than the time-honored scientific method, applied to the study of algorithms.
Suppose you have implemented several sorting algorithms, to validate this hypothesis, we use SortCompare to perform the experiments. We use Stopwatch to compute the running time. The implementation of time() shown here does the job for the basic sorts in this chapter. The “randomly ordered” input model is embedded in the timeRandomInput() method in SortCompare, which generates random Double values, sorts them, and returns the total measured time of the sort for the given number of trials. Using random Double values between 0.0 and 1.0 is much simpler than the alternative of using a library function such as StdRandom.shuffle() and is effective because equal key values are very unlikely. The number of trials is taken as an argument both to take advantage of the law of large numbers (the more trials, the total running time divided by the number of trials is a more accurate estimate of the true average running time) and to help damp out system effects.
public class Stopwatch { private final long start; /** * Create a stopwatch object. */ public Stopwatch() { start = System.currentTimeMillis(); } /** * Return elapsed time (in seconds) since this object was created. */ public double elapsedTime() { long now = System.currentTimeMillis(); return (now - start) / 1000.0; } }
/** * Replace this line with class description. * <p/> * User: George Sun * Date: 9/15/13 * Time: 9:45 PM */ public class SortCompare { 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 t1 = timeRandomInput(alg1, N, T); double t2 = timeRandomInput(alg2, N, T); StdOut.printf("For %d random Doubles\n %s is: ", N, alg1); StdOut.printf(" %1f times faster than %s\n", t2 / t1, alg2); } 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, Comparable[] a) { Stopwatch timer = new Stopwatch(); switch (alg) { case "Insertion": Insertion.sort(a); break; case "Selection": Selection.sort(a); break; case "Shell": Shell.sort(a); break; case "Merge": Merge.sort(a); break; case "Quick": Quick.sort(a); break; case "Heap": Heap.sort(a); break; case "InsertionSentinel": InsertionWithSentinel.sort(a); break; default: System.err.println("No algorithm specified, end."); break; } return timer.elapsedTime(); } }
相关推荐
“Analyzing and Comparing Montgomery Multiplication Algorithms”(分析与比较蒙哥马利模乘算法)这篇文章旨在深入探讨并对比不同的蒙哥马利模乘算法实现方法。蒙哥马利模乘算法是一种在计算机科学中广泛应用的...
comparing of autofocus algorithms
It helps in comparing the efficiency of different algorithms by analyzing how their running times scale with input size. 4. **Big-Oh Notation**: Big-Oh notation is a mathematical notation used to ...
标题 "JDBC_sorting_Comparing_" 暗示了这个主题是关于在Java环境中使用JDBC(Java Database Connectivity)进行数据排序,并且涉及到对象的`equals`和`hashCode`方法的重写。`equals`和`hashCode`方法是Java中的...
在当今的数据挖掘领域,监督式机器学习算法已经成为主流方法。利用健康数据进行疾病预测是这些方法的潜在应用场景之一。本文的目的是识别不同种类的监督式机器学习算法之间在疾病风险预测上的关键趋势、性能和使用...
Increased quantitative information about the algorithms, giving you a basis for comparing them Over 1000 new exercises to help you learn the properties of algorithms Whether you are learning the ...
multiway radix sorting, randomized BSTs, splay trees, skip lists, multiway tries, B trees, extendible hashing, and much more * Increased quantitative information about the algorithms, giving you a ...
比较算法 比较算法类的课程材料 第 1 课 - 搜索和简单递归 介绍是如何计算几个搜索算法和一些著名的递归怪物的复杂性。 第 2 课 - 数据结构 链表、二叉树、堆和数组之间的区别。 第 3 课 - 排序 ...
we give some experimental results comparing a few of the algorithms discussed in this paper. Keywords: Boosting algorithms, multiclass classification, output coding, decision trees
Abstract: With the development of engineering technology and the improvement of mathematical model, a large number of optimization ...the algorithm was verified by comparing with other algorithms
Abstract: With the development of engineering technology and the improvement of mathematical model, a large number of optimization ...the algorithm was verified by comparing with other algorithms.
本项目“Hadoop-comparing-two-text-files”聚焦于使用Hadoop MapReduce来比较两个文本文件,这对于数据比对、日志分析、数据一致性验证等场景非常有用。Hadoop基于Java编程语言实现,因此在编写MapReduce程序时,...
随书前言如下:Learn Data Structures and Algorithms with Go covers topics related to simple and advanced concepts in computer programming. The primary objective is to choose the correct algorithm and ...
下面将详细阐述哈希码(hashCode)和比较(Comparing)在Java中的作用以及它们在对象比较中的应用。 首先,哈希码是一个整数值,由对象的内部状态计算得出,通常用于快速查找。`hashCode()`方法是每个Java对象的...
在讨论低功耗无线技术时,本文将对比包括Bluetooth低功耗(BLE)、ANT、ANT+、ZigBee、ZigBee RF4CE、Wi-Fi、Nike+、IrDA和近场通信(NFC)标准在内的多种无线技术。首先,我们将详细解析每种技术的特性、优点以及在...
在内容部分中,提到的文章“Characterizing and Comparing Phylogenies from their Laplacian Spectrum”发表在系统生物学杂志(Systematic Biology)上,卷号为65,期号为3,页码范围495-507。文章是通过拉普拉斯谱...
本文《Attention Flows:Analyzing and Comparing Attention Mechanisms in Language Models》是一篇研究论文,主要关注于深度学习领域内语言模型的注意力机制。随着自然语言处理(NLP)技术的发展,基于注意力机制...
CAN FD 传统CAN之比较 CAN-FD比CAN总线的带宽更高,具有与CAN总线相似的控制器接口,这种相似性使ECU供应商不需要对ECU的软件部分做大规模修改,降低了开发难度和成本。CAN-FD是CAN总线的升级换代设计,它继承了CAN...