0% found this document useful (0 votes)
26 views5 pages

Algorithms - Weekly Test 03 - Test Paper

Uploaded by

Am kae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Algorithms - Weekly Test 03 - Test Paper

Uploaded by

Am kae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Branch : CSE/IT Batch : Hinglish


WEEKLY TEST – 03
Subject : Algorithm
Topic : Design Strategy
Maximum Marks 15

Q.1 to 5 Carry ONE Mark Each


[MCQ] program. Would require to make before finding the key
n _______________(if the key is present ,round of to
1. Assume that there are 4n sorted list of size   , then
2 two decimal.)
what is the time complexity of merging them into
single sorted list [MSQ]
(a) θ(n2 logn) (b) (log n) 4. Choose the correct statements from the following
(c) θ(nlogn) (d) θ(n2) statements
(a) Binary search in an unsorted array will take
[MSQ]
O(nlogn) time in worst case.
2. Assume that, quick sort implementation is used to sort
an array in ascending order after the first partition step (b) Searching for an element in an unsorted array will
has been completed, the contents of the array are in the take O(n) time in the worst case.
following order. (c) Binary search on a sorted linked list takes O(n)
50 30 40 20 80 90 120 100 140 110 160 time in worst case.
Which of the following elements could be a pivot (d) Applying binary search on sorted linked list takes
element? O(logn) time in worst case.
(a) 40 (b) 30
(c) 80 (d) 160
[MCQ]
[NAT] 5. What is the auxiliary space complexity of merge sort
3. Assume that a binary search is used to search for a (a) O(1) (b) O(logn)
particular key within a sorted array of 256 values then (c) O(n) (d) O(nlog)
what is the maximum number of key comparisons, the

Q.6 to 10 Carry TWO Mark Each


[NAT] for (b = 2; b< = a; b++)
6. Generally, merge is a divide and conquer technique {
which can also be implemented in a recursive manner. if(b%i = = 0)
If there are 200 elements in an array then find the exact printf (“GATEWALLAH”);
number of merge sort function calls which will perform }
the recursive call______. }
Which of the following statements is/are true?
[MSQ] (a) If f(n) is the number of times “GATEWALLAH”
7. Consider the following code segment printed in terms of n, then f(n) is equivalent to n
(b) If f(n) is the number of times “GATEWALLA”
(Note: n is power of 2 and base of log is 2)
is printed in terms of n then f(n) is equivalent to
int a, b, n; n–1
for (a = 2; a < 2logn; a++) (c) Time complexity of the given code is θ(n)
{ (d) Time complexity of the given code is θ(n2)
2

[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

Hints and Solutions

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

8. (a) ∴ cost of every level is 2n × n


let T(n) be the time of sorting the list using merge Total number of levels = k + 1 {20 to 2k}
sort. Overall cost = 2n × n [k + 1]
Given that list has to be divided into 3 equal parts and
2n
perform sorting in each sub lists Here =1
2k
So, T(n) = T(n/3) + T(n/3) + T(n/3)
2k = 2n
After performing sorting, we need to copy the list again
Taking log on both sides
into the original list which will take (n – 1) units or
k=n
work.
∴ overall cost = 2n × n (n + 1)
T(n) = 3T(n/3) + n – 1
= 2n × n2 + 2n × n = O(n2 × 2n)
∴ option (a) is correct.
∴ (a) is correct option.
9. (a)
10. (d)
Apply binary search
If a[mid] > a [right & a[mid] > a[left]
Then max = a[mid]
if a[mid] > a [right] & a [mid] < a [left] → binary search
(start, mid -1)
if a[mid] <a[right] & a[mid] > a[left] → binary search
(mid+1, last)
Cost of level 0 2n × n
2n
Cost of level 1 is  2n = 2n × n
2

For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if

PW Mobile APP: https://smart.link/7wwosivoicgd4

You might also like