今天做完了Problem Sets和Programming Questions。贴出来共享一下吧。
Problem Sets:Question 1
3-way-Merge Sort : Suppose that instead of dividing in half at each step of Merge Sort, you divide into thirds, sort each third, and finally combine all of them using a three-way merge subroutine. What is the overall running time of this algorithm? (Hint: Note that the merge step can still be implemented in O(n) time.)
Answer:nlog(n)
Question 2 You are given functions f and g such that f(n)=O(g(n)). Is f(n)∗log2(f(n)c)=O(g(n)∗log2(g(n))) ? (Here c is some constant >0. You should assume that f and g are nondecreasing and always bigger than 1.
Answer:True
Question 3
Assume again two (positive) functions f and g such that f(n)=O(g(n)). Is 2f(n)=O(2g(n)) ? (Multiple answers may be correct.)
Answer:Sometimes and Yes if f(n)≤g(n) for all sufficiently large n
Question 4
k-way-Merge Sort. Suppose you are given k sorted arrays, each with n elements, and you want to combine them into a single array of kn elements. Consider the following approach. Using the merge subroutine taught in lecture, you merge the first 2 arrays, then merge the 3rd given array with this merged version of the first two arrays, and so on until you merge in the final (kth) input array. What is the running time taken by this strategy, as a function of k and n? (Optional: can you think of a faster way to do the k-way merge procedure ?)
Answer:θ(nk^2)
Question 5 Arrange the following functions in increasing order of growth rate (with g(n) following f(n) in your list if and only if f(n)=O(g(n))).
a)(n^2)log(n)
b)2^n
c)2^(2^n)
d)n^(log(n))
e)n^2
Write your 5-letter answer, i.e., the sequence in lower case letters in the space provided. For example, if you feel that the answer is a->b->c->d->e, then type abcde in the space provided without any spaces before / after / in between the string. You can assume that all logarithms are base 2 (though it actually doesn't matter). WARNING: this question has multiple versions, you might see different ones on different attempts!
Answer:eadbc
其中Question 5 中需要排列的几项因人而异。
Programming Questions:
Question 1 Download the text file here. (Right click and save link as) This file contains all of the 100,000 integers between 1 and 100,000 (inclusive) in some random order, with no integer repeated. Your task is to compute the number of inversions in the file given, where the ith row of the file indicates the ith entry of an array.
Because of the large size of this array, you should implement the fast divide-and-conquer algorithm covered in the video lectures. The numeric answer for the given input file should be typed in the space below.
So if your answer is 1198233847, then just type 1198233847 in the space provided without any space / commas / any other punctuation marks. You can make up to 5 attempts, and we'll use the best one for grading.
(We do not require you to submit your code, so feel free to use any programming language you want --- just type the final numeric answer in the following space.) [Tip: before submitting, first test the correctness of your program on some small test files or your own devising.] Answer:2407905288 程序可以参考第一期:Week#1Q&A Python Java C C++都有。
|