Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Algorithms and data structures
Correctness and Complexity
Question 1
For constants b and c, consider the recurrence relation given by:
T(n) = b, if n=1
T(n) = 2 * T(n/2) + c * n , if n>1
Which of the following statements is true?
Select one:
a. T(n) = Θ(n 3 * log n)
b. T(n) = Θ(n 4 )
c. T(n) = Θ(n 3 )
d. T(n) = Θ(n 6 * log n)
e. T(n) = Θ(n 3 * log n * log n * log n)
Question 2
Consider the following algorithm, which returnsTrue if and only if m is a factor of sum(L), where L is a list of integers.
def sum_factor(L, m):
"""
Input : A list L of integers, and an integer m
Output: True if m is a factor of sum(L), and False otherwise.
For example:
>>> sum_factor([3, 1, 2, 6], 3)
True
>>> sum_factor([4, 1, 2, 6], 3)
False
"""
n = len(L)
s = 0
for i in range(n):
s = (s + L[i]) % m
return s == 0
(a) Write down a useful invariant for this algorithm.
(b) Show that the invariant you wrote is true on loop entry, and each time the loop runs.
(c) Now use your invariant to argue that the algorithm is correct.
Sorting
Question 3
Consider an input with N elements.
Please select all correct statements regarding comparison-based sorting algorithms.
Select one or more:
a. The best-case complexity of insertion sort is O(N).
b. The best-case complexity of merge sort is O(N).
c. Heap sort is stable.
d. Selection sort is stable.
e. The average-case complexity of selection sort is θ(N2).
Quickselect and Median of Medians