For many programs, developing a mathematical model of running time
reduces to the following steps:
■Develop an input model, including a definition of the problem size.
■ Identify the inner loop.
■ Define a cost model that includes operations in the inner loop.
■Determine the frequency of execution of those operations for the given input.
Doing so might require mathematical analysis—we will consider some examples
in the context of specific fundamental algorithms later in the book.
If a program is defined in terms of multiple methods, we normally consider the
methods separately. As an example, consider our example program of Section 1.1,
BinarySearch .
Binary search. The input model is the array a[] of size N; the inner loop is the
statements in the single while loop; the cost model is the compare operation
(compare the values of two array entries); and the analysis, discussed in Section
1.1 and given in full detail in Proposition B in Section 3.1, shows that the num-
ber of compares is at most lg N ? 1.
Whitelist. The input model is the N numbers in the whitelist and the M numbers
on standard input where we assume M >> N; the inner loop is the statements in
the single while loop; the cost model is the compare operation (inherited from
binary search); and the analysis is immediate given the analysis of binary search—
the number of compares is at most M (lg N ? 1).
Thus, we draw the conclusion that the order of growth of the running time of the
whitelist computation is at most M lg N , subject to the following considerations:
■ If N is small, the input-output cost might dominate.
■The number of compares depends on the input—it lies between ~M and ~M
lg N, depending on how many of the numbers on standard input are in the
whitelist and on how long the binary search takes to find the ones that are (typi-
cally it is ~M lg N ).
■ We are assuming that the cost of Arrays.sort() is small compared to M lg N.
Arrays.sort() implements the mergesort algorithm, and in Section 2.2, we
will see that the order of growth of the running time of mergesort is N log N
(see Proposition G in chapter 2), so this assumption is justified.
Thus, the model supports our hypothesis from Section 1.1 that the binary search algo-
rithm makes the computation feasible when M and N are large. If we double the length
of the standard input stream, then we can expect the running time to double; if we
double the size of the whitelist, then we can expect the running time to increase only
slightly.

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  2. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

  3. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-007按位置,找出数组相关最大值

    Given an array a[] of N real numbers, design a linear-time algorithm to find the maximum value of a[ ...

  4. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-006BitonicMax

    package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; /*** ...

  5. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-004计算内存

    1. 2. 3.字符串

  6. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-003定理

    1. 2. 3. 4. 5. 6.

  7. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

随机推荐

  1. LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  3. Python ord()与chr()函数

    chr():十进制或十六进制数(0-255)转成对应的ASCII字符. ord():ASCII字符转成对应的十进制数. 一个小性质:ASCII表中大写字母排在前面小写排在后面,相差32. 比如: or ...

  4. poj 3463 Sightseeing——次短路计数

    题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...

  5. 五、Jmeter--关联(正则表达式)

    一.什么时候需要关联? 1. 服务器返回的动态变化而且对业务有影响的需要关联. 2. 回放脚本看是否正确,检查下脚本,是否有动态数据影响 3. 一大串字符串,每次请求参数是否有变化 4. 可以找开发问 ...

  6. shell脚本中常用命令

    1           Shell中的特殊符号 1.1           $  美元符号.用来表示变量的值.如变量NAME的值为Mike,则使用$NAME就可以得到“Mike”这个值. 1.2    ...

  7. setcookie函数的注意事项

    函数说明 bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $pat ...

  8. SpringMVC的环境搭建

    MyBatis框架-->持久层框架-->Object[对象]Relation[关系型数据库]Mapping[在MyBatis的体现是哪个映射文件中国的<resultMap>标签 ...

  9. DataX实现oracle到oracle之间的数据传递

    首先需要注意的是DATAX是通过JDBC的方式读取ORACLE数据,然后通过OCI的方式写数据,DX也可以通过JDBC写的方式进行,但是OCI比JDBC速度更快.   进入DataX安装目录的bin目 ...

  10. [转] linux 启动文件及设置环境变量

    系统级启动文件  ==================================== 1./etc/rc  主启动文件,不要修改它 2./etc/rc.conf  决定启动哪些系统自带的守护进程 ...