Algorithms - Weekly Test 03 - Test Paper
Algorithms - Weekly Test 03 - Test Paper
[MCQ] [MCQ]
8. Consider a variation of merge sort in which we divide 9. Consider a list which contains 2n sorted lists each of
the list into 3 sorted sub lists of equal size, recursively size n and is merged using merge sort, then what is the
sorting each list, and then merging the three lists to get tightest upper bound worst case complexity?
the final sorted list. (a) O(n22n) (b) O(2nlogn)
n
What is the recurrence relation that is required for the (c) O(n.2 ) (d) None of these
number of comparisons used by this algorithm in worst
case? [MCQ]
(NOTE: Assume that the number of elements to be 10. Let’s suppose you are given an array in which, the few
sorted is a power of 3 so that all of the divisions are elements in the beginning are present in ascending
into three sub lists workout evenly) order and remaining elements are in descending order,
(a) T(n) = 3T(n/3) + n – 1 then what is the complexity of most efficient algorithm
(b) T(n) = 2T (n/2) + n – 1 to find the maximum value of this array?
(c) T(n) = 6T (n/3) + n – 1 (a) θ(n logn) (b) θ(n)
(d) None of these (c) O(1) (d) θ(log n)
3
Answer Key
1. (a) 6. (398 to 398)
2. (c, d) 7. (b, d)
3. (8 to 8) 8. (a)
4. (a, b, c) 9. (a)
5. (c) 10. (d)
4
1. (a) 5. (c)
_ _ _ _ _ _ _ _ An additional space of O(n) is required in order to
4n sorted list merge two sorted arrays. Thus, merge sort is not an in-
place sorting algorithm.
_ _ _ _ _ _ _ _
2n sorted list (log n + 2)levels 6. (398 to 398)
.
.
.
n=7
1 list
n
Merging time complexity at each level = 4n = 2n
2
∴ 2n2 (logn + 2) = θ (n2 logn)
2. (c, d)
All the elements before 80 are less than 80 and
elements after 80 are greater than so 80 could be a pivot For n = 1 → 0 calls
element. n = 2 → 2 calls
All elements before 160 are less than 160, s0 160 could n = 3 → 4 calls
also be a pivot element. n = 4 → 6 calls
∴ option (c, d) are correct. ∴ generalized formula is 2(n–1) calls
So, for n = 200,
3. (8 to 8) 2(200 – 1) = 2 (199) = 398
The maximum number of comparisons in a binary
search on sorted array of 256 elements are log2n, 7. (b, d)
As we can see that,
where n is the number of elements.
Whenever “b = i” in inner loop, “GATEWALLAH”
∴ log2256, = 8
will be printed, so “GATEWALLAH” will be printed
∴ 8 number of comparisons are required (This include
energy time once for i = 2 to i = n ie,.. (n – 1) times
the case where elements not present in the array)
f(n) = (n – 1)
Also, for (i = 2) inner loop will execute for 2 times
4. (a, b, c)
for (i = 3) inner loop will execute for 3 times.
(a) Yes, Binary search in an unsorted array will take
for (i = n) inner loop will execute for n times.
O(nlogn) time in worst case.
So, total complexity of given code is
(b) Yes, Searching an elements in an unsorted array
n ( n + 1)
will take O(n) time in the worst case. 2 + 3 + 4 …… n ⇒ –1 = θ(n2)
2
(c) Yes, Binary search on an sorted linked list takes
∴ option b, d are correct.
O(n) time in worst case.
5
For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if