为什么需要分析算法?

  1. 对性能进行预测
  2. 进行算法比较
  3. 为结果提供保障
  4. 理解理论基础

双对数(以2为底)坐标作图,横坐标是数据量的对数,纵坐标是运行时间的对数,得到的斜率 k 的值意味着 数据量每增加一倍,所要花费的时间大约是之前运行时间的2^k倍。比如斜率为3,1k用时为1s,那么2k用时就为1*2^3 s = 8 s
image.png
幂定律
image.png

a 取决于电脑本身的硬件和软件等
b 取决于算法

数学模型

image.png
image.png

计算如下代码片段数组的访问次数

int sum = 0;
for (int i = 0; i < n; i++)
    for (int j = i+1; j < n; j++)
        for (int k = 1; k < n; k = k*2)
            if (a[i] + a[j] >= a[k]) sum++;

前面两个循环共执行如下次数
image.png

第三个循环中,k 的值依次是
image.png
注:
image.png

故第三个循环共执行如下次数:
image.png

所以整个数组的访问次数 = 前两个循环的循环次数 * 第三个循环的次数 * 每次循环访问数组的次数

image.png

增长阶数

image.png

二叉搜索分析
public static int binarySearch(int[] a, int key)
{
    int lo = 0, hi = a.length-1; 
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (key < a[mid]) hi = mid - 1;
        else if (key > a[mid]) lo = mid + 1;
        else return mid; 
    } 
    return -1; 
}
优化后的 3-sum 算法(复杂度为 N^2 logN)

image.png

Theory of Algorithms

image.png

O 增长阶数的上界
Ω 增长阶数的下界
Θ 增长阶数的上下界
image.png

关于 Which of the following function is O(n^3) ?
该问题为什么全选,coursera的导师Beau Dobbin是这样回答的

Getting familiar with Big "O" notation takes a little while. The key to remember is that Big "O" defines an upper bound for a growth rate. It can seem confusing that such different functions have the same Big "O" _order_.

In other words, as all three functions approach infinite, they are all less than or equal to "n^3". You could also say that all three are O(n^789457398). As long as the Big "O" function grows faster (or equal), the notation holds.

Similarly, Big Omega "Ω" and Theta "Ө" notations represent lower bounds and _simultaneous upper and lower bounds_.

In other words:

If your algorithm "runs in O(n^2) time", it takes at most (n^2) time.

If your algorithm "runs in Ω(n^2) time", it takes at least (n^2) time.

If your algorithm "runs in Ө(n^2) time", it takes at most (n^2) time and at least (n^2) time. This is similar to saying "it runs in exactly (n^2) time," but it's not exact. It's the asymptotic time as n approaches infinite.

https://en.wikipedia.org/wiki/Big_O_notation : Look for "A description of a function in terms of big O notation usually only provides an upper bound on the growth rate of the function."[](https://www.desmos.com/calcul...

[](https://en.wikipedia.org/wiki...https://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachmann.E2.80.93Landau_notations : A comparison of a few common notations.

https://www.desmos.com/calculator/7vmsklh8o9 : A graph of the three functions.

memory

image.png

  • Object overhead. 16 bytes.
  • Reference. 8 bytes.
  • Padding. Each object uses a multiple of 8 bytes

image.png
image.png


ahtlzj
1 声望1 粉丝

人工智能成长ing, 关注我就是关注一个潜力股