2.2 分析算法
2-1
Θ(n³)Θ(n³)Θ(n³)
2-2
Selection-Sort(A)
1 for i = 1 to A.length - 1
2 Subscript = i // 存储下标
3 for j = i to A.length - 1
4 if A[Subscript] > A[j]
5 Subscript = j // 存储元素数据更小的数组下标
6 Data = A[Subscript] // 交换数据
7 A[Subscript] = A[i] // 交换数据
8 A[i] = Data // 交换数据
- 该算法的循环不变式:A[1…i]总是排好序的,Subscript总是A[i…n]中最小元素数据的下标
- 因为当前面的A[i…n - 1]已经是排序好的,且留在最后的一个元素中的数据一定是整个数组中最大的,所以可以省略掉最后一个的排序
- 每条语句的运行之和为:
Selection-Sort(A)
1 for i = 1 to A.length - 1 c1 n
2 Subscript = i c2 n - 1
3 for j = i to A.length c3 ①
4 if A[Subscript] > A[j] c4 ②
5 Subscript = j c5 ②
6 Data = A[Subscript] c6 n - 1
7 A[Subscript] = A[i] c7 n - 1
8 A[i] = Data c8 n - 1
① ∑j=2ntj\sum ^{n}_{j=2}t_{j}∑j=2ntj
② ∑j=2n(tj−1)\sum ^{n}_{j=2}\left( t_{j}-1\right)∑j=2n(tj−1)
该算法的运行时间(执行每条语句的运行时间之和)是:
T(n)=C1n+C2(n−1)+C3∑j=2ntj+C4∑j=2n(tj−1)+C5∑j=2n(tj−1)+C6(n−1)+C7(n−1)+C8(n−1)T\left( n\right) =C_{1}n+C_{2}\left( n-1\right)+C_{3}\sum ^{n}_{j=2}t_{j}+C_{4}\sum ^{n}_{j=2}\left( t_{j}-1\right)+C_{5}\sum ^{n}_{j=2}\left( t_{j}-1\right)+C_{6}\left( n-1\right)+ C_{7}\left( n-1\right)+ C_{8}\left( n-1\right)T(n)=C1n+C2(n−1)+C3∑j=2ntj+C4∑j=2n(tj−1)+C5∑j=2n(tj−1)+C6(n−1)+C7(n−1)+C8(n−1)
其中我们注意到:
∑j=2ntj\sum ^{n}_{j=2}t_{j}∑j=2ntj = (n(n+1)2−1)\left( \dfrac {n\left( n+1\right) }{2}-1\right)(2n(n+1)−1)
∑j=2n(tj−1)\sum ^{n}_{j=2}\left( t_{j}-1\right)∑j=2n(tj−1) = n(n−1)2\dfrac {n\left( n-1\right)}{2}2n(n−1)
最佳情况和最坏情况一样:
T(n)=C1n+C2(n−1)+C3(n(n+1)2−1)+C4(n(n−1)2)+C5(n(n−1)2)+C6(n−1)+C7(n−1)+C8(n−1)T\left( n\right) =C_{1}n+C_{2}\left( n-1\right)+C_{3}\left( \dfrac {n\left( n+1\right) }{2}-1\right)+C_{4}\left( \dfrac {n\left( n-1\right)}{2}\right)+C_{5}\left( \dfrac {n\left( n-1\right)}{2}\right)+C_{6}\left( n-1\right)+C_{7}\left( n-1\right)+C_{8}\left( n-1\right)T(n)=C1n+C2(n−1)+C3(2n(n+1)−1)+C4(2n(n−1))+C5(2n(n−1))+C6(n−1)+C7(n−1)+C8(n−1)
=(C32+C42+C52)n2+(C1+C2+C32−C42−C52+C6+C7+C8)n−(C2+C3+C6+C7+C8)=\left( \dfrac {C_{3}}{2}+\dfrac{C_{4}}{2}+\dfrac{C_{5}}{2}\right)n^{2}+\left(C_{1}+C_{2}+\dfrac{C_{3}}{2}-\dfrac{C_{4}}{2}-\dfrac{C_{5}}{2}+C_{6}+C_{7}+C_{8}\right)n-\left(C_{2}+C_{3}+C_{6}+C_{7}+C_{8}\right)=(2C3+2C4+2C5)n2+(C1+C2+2C3−2C4−2C5+C6+C7+C8)n−(C2+C3+C6+C7+C8)
我们可以表示为:an2+bn+can^{2}+bn+can2+bn+c
忽略掉低阶项和最重要的项的常系数时,只剩下重要的项中的因子n2n^{2}n2,得出运行时间为Θ(n2)Θ\left(n^{2}\right)Θ(n2)
2-3
-
平均情况:Θ(n)Θ\left(n\right)Θ(n)
最坏情况:Θ(n)Θ\left(n\right)Θ(n) -
1-3代码如下:
FIND-SORT(A, u)
1 for i = 1 to A.length c1 n + 1
2 if u == A[i] c2 n
3 return A[i] c3 1
4 return NIL c4 1
最坏情况:
该算法的最坏情况为最后一个元素为我们需要找的数据。
运行时间(执行每条语句的运行时间之和)是:
T(n)=C1(n+1)+C2n+1+1T\left(n\right)=C_{1}\left(n+1\right)+C_{2}n+1+1T(n)=C1(n+1)+C2n+1+1
=(C1+C2)n+C1+2=\left(C_{1}+C_{2}\right)n+C{1}+2=(C1+C2)n+C1+2
我们可以表示为:an+b+can+b+can+b+c
忽略掉低阶项和最重要的项的常系数时,只剩下重要的项中的因子nnn,得出运行时间为Θ(n)Θ\left(n\right)Θ(n)
平均情况:
平均情况下,我们找到每一个元素的几率为1n\dfrac{1}{n}n1,之所以加1,是因为存在没有找到的情况。我们将每个元素需要寻找的次数加在一起,也就是为∑j=1ntj\sum^{n}_{j=1}t_{j}∑j=1ntj,然后除去nnn,如下:
T(n)=(n(n+1)2)/(n)=(n+1)2=12n+12T\left(n\right)=\left(\dfrac{n\left(n+1\right)}{2}\right)/\left(n\right)=\dfrac{\left(n+1\right)}{2}=\dfrac{1}{2}n+\dfrac{1}{2}T(n)=(2n(n+1))/(n)=2(n+1)=21n+21
我们可以表示为:an+ban+ban+b
忽略掉低阶项和最重要的项的常系数时,只剩下重要的项中的因子nnn,得出运行时间为Θ(n)Θ\left(n\right)Θ(n)
2-4
We can modify it to handle the best-case efficiently. For example, if we modify merge-sort to check if the array is sorted and just return it, the best-case running time will be Θ(n).
我们可以对其进行修改以有效地处理最佳情况。例如,如果我们修改合并排序以检查数组是否已排序并只返回它,则最佳情况下的运行时间为Θ(n)Θ\left(n\right)Θ(n)。
本文深入分析了算法的运行时间,分别探讨了2.2节中的不同算法在最佳、平均和最坏情况下的时间复杂度。通过对循环不变式和运行时间的详细计算,得出算法的运行时间是Θ(n²)和Θ(n)。最后,提出了通过修改算法以优化最佳情况运行时间的方法。
968

被折叠的 条评论
为什么被折叠?



