Skip to content

Commit ee4c10f

Browse files
committed
update 2.2-2 2.2-3 2.2-4
1 parent fcbdd85 commit ee4c10f

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

C02-Getting-Started/2.2.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Consider sorting n numbers stored in array A by first finding the smallest eleme
1414
### `Answer`
1515
![pic](./repo/s2/1.png)
1616

17+
Loop invariant: at the start of each iteration of the outer for loop, the subarray A[1..j-1] consists of the j-1 smallest elements in the array A[1..n] and this subarray is in sorted order.
18+
19+
After the first n-1 elements, the subarray A[1..n-1] contains the smallest n-1 elements, sorted, and therefore element A[n] must be the largest element
20+
1721
时间都是Θ(![](http://latex.codecogs.com/gif.latex?n^2))
1822

1923

@@ -22,10 +26,10 @@ Consider sorting n numbers stored in array A by first finding the smallest eleme
2226
Consider linear search again (see Exercise 2.1-3). How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in Θ-notation? Justify your answers.
2327

2428
### `Answer`
25-
* 平均情况应该要查找n/2个元素
29+
* 平均情况应该要查找(n+1)/2个元素
2630
* 最坏情况是n个
2731

28-
* Assuming equal probability of occurrence, average number of elements which need to be checked is n/2. Running time is Θ(n)
32+
* Assuming equal probability of occurrence 1/n, average number of elements which need to be checked is 1/n * (1 + 2 + ... +n) = (n+1)/2. Running time is Θ(n)
2933
* Worst case, the element to search is dead last in the array. In that case n elements need to be searched. Running time is Θ(n)
3034

3135
所以都是Θ(n)
@@ -36,9 +40,9 @@ Consider linear search again (see Exercise 2.1-3). How many elements of the inpu
3640
How can we modify almost any algorithm to have a good best-case running time?
3741

3842
### `Answer`
39-
投机取巧,往最好情况去想
43+
算法开始检测输入数据,若符合特殊条件则输出事先计算好的结果
4044

41-
One could check whether the problem is already solved (when sorting, for instance, check if input is already sorted).
45+
Modify the algorithm so it checks whether the input satisfies some special case condition. If it does, output a pre-computed answer.
4246

4347

4448

0 commit comments

Comments
 (0)