Chapter 1 - Algorithm Analysis(b)
定义
注意
补充、解释
其他
0 Solution to Exercise
对于程序1,$i=0$,最内层语句执行0次,$i=1$,最内层语句执行1次,以此类推
最内层语句执行$$1+2+…+n-1=O(n^2)$$
对于程序2,注意第二层循环的条件,$i$必须在$0\le i \le n-1$时第二层循环内部的语句才会执行,且重复$n-i$次
因此复杂度为$$n\sum^{n-1}_{i=0}(n-i)=O(n^3)$$
对于$i$的其他取值,判断一下后不会执行内层循环,判断执行$n^3/2 - n$次
故时间复杂度为O(n^3)
1 Compare the Algorithms
Given(possibly negative) integers $A_1,A_2…,A_n$,find the maximum value of $\sum_{k=i}^jA_k$
1.1 Algorithm 01
1 | //Algorithm 01 |
三层循环总共运行的次数为
$$
\sum_{i=0}^{n-1}\sum_{j=i}^{n-1}\sum_{k=i}^{j}1
$$
$$
\sum_{k=i}^{j}1=j-i+1
$$
$$
\sum_{j=i}^{n-1}(j-i+1)=1+2+…+n-i
$$
$$
=(n-i)(n+1-i)/2
$$
$
\sum_{i=0}^{n-1}(n-i)(n+1-i)
$
$=n^2+n-(2n+1)\sum_{i=0}^{n-1}i+\sum_{i=0}^{n-1}i^2$
$=n^2+n-n(2n+1)(n-1)/2+n(n-1)(2n-1)/6$
故$T(n)=O(n^3)$
1.2 Algorithm 02
1 | /*上面的算法,如i=0,它是这么算的,从第0项加到第1项 |
$T(n)=O(n^2)$
1.3 Algorithm 03
分而治之
1 | int MaxSubsequenceSum(const int a[],int n) |
1.4 Algorithm 04(on-line algorithm)
1 | int MaxSubsequenceSum(const int a[],int n) |
$T(N)=O(N)$
可以用-1 3 -2 4 -6 1 6 -1试试理解
在线处理的特点是每输入一个数据就能做到即时处理,在任何一个地方中止输入,算法都能正确地给出当前的解
2 Logarithms in the Running Time
Besides divide-and-conquer algorithms, the most frequent appearance of logarithms centers around the following general rule: An algorithm is O(log n) if it takes constant (O(1)) time to cut the problem size by a fraction (which is usually ).
On the other hand, if constant time is required to merely reduce the problem by a constant amount (such as to make the problem smaller by 1), then the algorithm is O(n)
(From textbook)
2.1 Binary Search
见上一节
2.2 Euclid’s Algorithm
1 | int gcd(int m,int n) |
例如50%15=5,50/15=3
15%5=0,return 5
If $m>n$,then $m (mod n)<\frac{m}{2}$
Proof:
There are two cases.
If $n\le m/2$,then the remainder is less than $n$,so it’s less than $m/2$
If $n>m/2$,then $m/n=1$,the remainder must be $m-n<m/2$
根据算法,两次循环后,m就会变为m%n,所以两次循环后m至少减半,而我们知道一旦开始循环,一次循环中,m的值就是上一次n的值
如果k次循环后,循环结束之后m=1,说明这次循环n=1,那么余数(rem)一定是0,n变为0(rem),算法停止
m减半$\frac{k}{2}$次,那么$$\frac{m}{2^{\frac{k}{2}}}=1$$
$$\frac{k}{2}=log_2m$$
所以时间复杂度是$O(logn)$
2.3 Exponentiation
比较直接的方法是乘n次x,复杂度为$O(N)$
1 | //Efficient Exponentiation |
n每次调用都会减半,调用$log_2n$次,每次调用要么做一次乘法算x*x,要么做两次乘法(n为奇数的情况再乘x),所以最多$2logn$次乘法运算
时间复杂度为$O(logN)$
3 Exercise
分析下面程序的时间复杂度
1 | int pow(int x,int n) |
- Title: Chapter 1 - Algorithm Analysis(b)
- Author: Hare Fuyukawa
- Created at : 2026-03-11 17:43:10
- Updated at : 2026-07-01 20:52:06
- Link: https://redefine.ohevan.com/Data-Structure/DS/FDS-2/
- License: This work is licensed under CC BY-NC-SA 4.0.