算法导论第三版第二章答案
第二章
1-1 1-2 1-3 1-4
2-1 2-2 2-3 2-4
3-1 3-2 3-3 3-4 3-5 3-6 3-7
1-1
Using figure 2-2 as a model, illustrate the operations of Insertion-Sort on the array A=?31,41,59,26,41,58?.
以圖2-2為模型,說明Insertion-Sort在數(shù)組A=?31,41,59,26,41,58?上的執(zhí)行情況
1-2
Rewrite the Insertion-Sort procedure to sort into nonincreasing instead of nondecreasing order.
重寫Insertion-Sort,使之按非升序排序
1-3
Consider the searching problem:
Input: A sequence of n n n numbers A=?a1,a2,…,an? and a value ν.
Output: And index i such that ν=A[i] or the special value NIL if ν does not appear in A.
Write the pseudocode for linear search, which scans through the sequence, looking for ν. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.
考慮以下查找問題:
輸入:n個數(shù)的一個序列A=?a1,a2,…,an?和一個值v
輸出:下標(biāo)i使得ν=A[i]或者當(dāng)v不在A中出現(xiàn)時,v為特殊值NIL
寫出線性查找的偽代碼,他掃描整個序列來查找v。使用一個循環(huán)不變式來證明你的算法是正確的。確保你的循環(huán)不變式滿足三條必要的性質(zhì)
循環(huán)不變式是A[1,j-1]中的所有元素都不同于v
初始化:A[1,j-1]為空,成立
保持:對于接下來的每一步我們都有A[1,j-1]中所有元素不同于v,因為一旦A[j]與v相同,我們就返回j
終止:循環(huán)終止條件是j>A.length,因此所以A中的數(shù)都比較過了,而v與它們都不同,所以返回NIL
1-4
Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array C. State the problem formally and write pseudocode for adding the two integers.
考慮把兩個n進制整數(shù)加起來的問題,這兩個整數(shù)分別存儲在兩個n元數(shù)組A和B中。把這兩個整數(shù)的和應(yīng)按二進制形式存儲在一個(n+1)元數(shù)組C中。請給出該問題的形式化描述,并寫出偽代碼
輸入:兩個n元二進制數(shù)組A<a1,a2…an>,B<b1,b2…bn>
輸出:n+1元二進制數(shù)組C,C滿足C=A+B
2-1
Express the function n3/1000?100n2?100n+3 in terms of Θ-notation
用Θ記號表示函數(shù)n3/1000-100n2-100n+3
Θ(n3)
2-2
Consider sorting n3 numbers in an array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A[2]. Continue in this manner for the first n?1 elements of A. Write pseudocode for this algorithm, which is known as selection sort. What loop invariants does this algorithm maintain? Why does it need to run for only the first n?1 elements, rather than for all n elements? Give the best-case and the worst-case running times of selection sort in Θ-notation.
考慮排序存儲在數(shù)組A中的n個數(shù):首先找出A中的最小元素并將其與A[1]中的元素進行交換。接著,找出A中的次最小元素并將其與A[2]中的元素進行交換。對A中前n-1個元素按該方式繼續(xù)。該算法稱為選擇算法,寫出其偽代碼。該算法維持的循環(huán)不變式是什么?為什么它只需要對前n-1個元素,而不是對所有n個元素運行?用Θ記號給出選擇排序的最好情況與最壞情況運行時間。
- 循環(huán)不變式是A[1,i-1]包含前i-1個最小的元素,且已經(jīng)排好序
- 為什么是n-1:因為前n-1個數(shù)排好序后,最后一個數(shù)一定是最大的,所以不用排它了
- 最好情況是(c1+c2+c6+c7+c8)(n-1)+(c3+c4)(n-1+n-2+…+1)=(c1+c2+c6+c7+c8)(n-1)+(c3+c4)(n-1)n/2
最壞情況是(c1+c2+c6+c7+c8)(n-1)+(c3+c4+c5)(n-1)n/2
顯然都是Θ(n2)
2-3
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 the worst case? What are the average-case and worst-case running times of linear search in Θ? Justify your answers.
再次考慮線性查找問題(參見練習(xí)2.1-3)。假定要查找的元素等可能地為數(shù)組中的任意元素,平均需要檢查輸入序列的多少元素?最壞情況又如何呢?用Θ記號給出線性查找的平均情況和最壞情況運行時間。證明你的答案。
平均需要檢查n/2個元素,運行時間均為Θ(n)
2-4
How can we modify almost any algorithm to have a good best-case running time?
我們可以如何修改任何一個算法,才能使之具有良好的最好情況運行時間?
進行每一步時檢查是否有必要進行,例如排序時檢查是否已經(jīng)排好序了
3-1
Using Figure 2-4 as a model, illustrate the operation of merge sort on the array A=?3,41,52,26,38,57,9,49?.
使用圖2-4作為模型,說明歸并排序在數(shù)組A=?3,41,52,26,38,57,9,49?上的操作。
3-2
Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once either array L or R has had all its elements copied back to A and then copying the remainder of the other array back into A.
重寫過程MERGE,使之不使用哨兵,而是一旦數(shù)組L或R的所有元素均被復(fù)制回A就立刻停止,然后把另一個數(shù)組的剩余部分復(fù)制回A
3-3
Use mathematical induction to show that when n is an exact power of 2, the solution of the recurrence is T(n)=nlgn.
T(n)={2n=22T(n/2)+nn=2k,k>1T(n) = \begin{cases} 2 & n = 2 \\ 2T(n/2)+n & n=2^k,k>1 \end{cases}T(n)={22T(n/2)+n?n=2n=2k,k>1?
使用數(shù)學(xué)歸納法證明:當(dāng)n剛好是2的冪時,以下遞歸式的解是T(n)=nlgn。
T(n)={2n=22T(n/2)+nn=2k,k>1T(n) = \begin{cases} 2 & n = 2 \\ 2T(n/2)+n & n=2^k,k>1 \end{cases}T(n)={22T(n/2)+n?n=2n=2k,k>1?
基礎(chǔ)步驟:T(21)=2=2lg2
歸納步驟:假設(shè)T(2n)=2nlg2n=n2n,則T(2n+1)=2T(2n)+2n+1=2n+1lg2n+1
3-4
We can express insertion sort as a recursive procedure as follows. In order to sort A[1…n], we recursively sort A[1…n?1] and then insert A[n] into the sorted array A[1…n?1]. Write a recurrence for the running time of this recursive version of insertion sort.
我們可以把插入排序表示為如下的一個遞歸過程。為了排序A[1.n],我們遞歸地排序A[1,n-1],然后把A[n]插入已排序的數(shù)組A[1,n-1]。為插入排序的這個遞歸版本的最壞情況運行時間寫一個遞歸式。
T(n)={Θ(1)n=1T(n?1)+Θ(n)n>1T(n) = \begin{cases} Θ(1) & n = 1 \\ T(n-1)+Θ(n) & n>1 \end{cases}T(n)={Θ(1)T(n?1)+Θ(n)?n=1n>1?
3-5
Referring back to the searching problem (see Exercise 2.1-3), observe that if the sequence A is sorted, we can check the midpoint of the sequence against ν and eliminate half of the sequence from further consideration. The binary search algorithm repeats this procedure, halving the size of the remaining portion of the sequence each time. Write pseudocode, either iterative or recursive, for binary search. Argue that the worst-case running time of binary search is Θ(lg?n)\Theta(\lg{n})Θ(lgn).
回顧查找問題(參見練習(xí)2.1-3),注意到,如果序列A已排好序,就可以將該序列的中點與v進行比較。根據(jù)比較的結(jié)果,原序列中有一半就可以不用再做進一步的考慮了。二分查找算法重復(fù)這個過程,每次都將序列剩余部分的規(guī)模減半。為二分查找寫出迭代或遞歸的偽代碼。證明:二分查找的最壞情況運行時間為Θ(lgn)
證明:由于T(n)=T(n/2)+常數(shù)c,T(1)=常數(shù)
則T(n)=T(2lgn)=T(2lgn-1)+c
T(2lgn-1)=T(2lgn-2)+c
……
T(2)=T(1)+c
所以T(n)=clgn-c+T(1),即二分查找的最壞情況運行時間為Θ(lgn)
3-6
Observe that the while loop line 5-7 of the INSERTION-SORT procedure in Section 2.1 uses a linear search to scan (backward) through the sorted subarray A[i…j?1]. Can we use a binary search (see Exercise 2.3-5) instead to improve the overall worst-case running time of insertion sort to Θ(nlg?n)?
注意到2.1節(jié)中的過程INSERTION- SORT的第5~7行的whle循環(huán)采用一種線性查找來(反向)掃描已排好序的子數(shù)組A[1,j-1]。我們可以使用二分查找(參見練習(xí)(2.3-5)來把插人排序的最壞情況總運行時間改進到e(nlgn)嗎?
不能,即使用二分查找找到了位置,也還是需要一個個的移動數(shù)組中的元素來進行插入
3-7
Describe a Θ(nlg?n)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exists two elements of S whose sum is exactly x.
描述一個運行時間為Θ(nlgn)的算法,給定n個整數(shù)的集合S和另一個整數(shù)x,該算法能確定S中是否存在兩個其和剛好為x的元素。
總結(jié)
以上是生活随笔為你收集整理的算法导论第三版第二章答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 10g 报错:ORA-00
- 下一篇: 小程序 上拉刷新 下拉加载 代码备忘