Daa Ct1 Set C Answer Key
Daa Ct1 Set C Answer Key
SET C
College of Engineering and Technology
School of Computing
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamil Nadu
Academic Year: 2023-24 (EVEN)
Part - A
(1 x 10 = 10 Marks)
Instructions: Answer all
Q. Question Marks BL CO PO PI
No Code
1 Number of comparisons required to search an element 1 2 1 1,4 4.4.2
x = 18 in the list A= [ 5, 44, 89, 22, 18, 9, 3, 15,8]
using linear search
a) 3
b) 1
c) 4
d) 5
Ans: 5
2 What is the time complexity of following code. 1 4 1 2,3 2.8.2
Assume that n > 0
int segment(int n) {
If(n==1)
return 1;
else
return (n+ segment(n-1)); }
a) O(n)
b) O(log n)
c) O(n2)
d) O(n!)
Ans: a
3 Which of the following functions provides the 1 2 1 2 2.8.2
maximum asymptotic complexity?
a) f1(n) = n^(3/2)
b) f2(n) = n^(logn)
c) f3(n) = nlogn
d) f4(n) = 2^n.
Ans: d.
4 Consider the following function. 1 2 1 4 4.6.2
int fun(int n)
{
int i,j;
for(i=1; i<=n; i++){
for(j=1; j<n; j += i){
printf(“%d %d”, i , j )
}}
}
a) Θ(n √n)
b) Θ (n2)
c) Θ (nlogn)
d) Θ (n2logn)
Ans: c
5 Derive the recurrence relation for the following code: 1 1 1 1 1.2.1
fact (int n)
{
if(n= = 0)
return 1;
else
return fact(n-1)*n;
}
A. T(n) = T(n-1)+n^2
B. T(n) = T(n)+n
C. T(n) = T(n-1)+1
D. T(n) = T(n-1)+n
Ans: C
6 What is the worst case time complexity of a quick sort 1 1 2 2 2.8.2
algorithm?
a) O(N)
b) O(N log N)
c) O(N2)
d) O(log N)
Ans: C
7 Which of the following sorting algorithms provide the 1 3 2 2,4 4.4.3
best time complexity in the worst-case scenario?
a) Merge Sort
b) Quick Sort
c) Bubble Sort
d) Selection Sort
Ans: a
8 Solve the following recurrence using Master’s 1 2 2 1 1.2.1
theorem.
T(n) = 4T (n/2) + n2
a) T(n) = O(n)
b) T(n) = O(log n)
c) T(n) = O(n2log n)
d) T(n) = O(n2)
Ans: c
9 Apply Quick sort on a given sequence 7 11 14 6 9 4 3 1 3 2 2 2.5.2
12. What is the sequence after first phase, pivot is first
element?
a) 6 4 3 7 11 9 14 12
b) 6 3 4 7 9 14 11 12
c) 7 6 14 11 9 4 3 12
d) 7 6 4 3 9 14 11 12
Ans: b
10 What is the time complexity of Largest subarray sum 1 1 2 2 2.8.2
problem using naïve approach
a) T(n) = O(n)
b) T(n) = O(log n)
c) T(n) = O(n2log n)
d) T(n) = O(n2)
Ans: d
Part – B
(5 x 4 = 20 Marks)
Instructions: Answer All the Questions
11 Express the function f(n) = n3/1000-100n2 - 100n + 3 5 2 1 1 1.2.1
in terms of Theta notation.
n3/1000 − 100n2 − 100n + 3 = Θ(n3)
Ans:
(Or)
Let n = 1, then
f(n) = 2n2+5 = 2(1)2+5 = 7
g(n) = 7(n) = 7(1) = 7
Here, f(n)=g(n)
Let n = 2, then
f(n) = 2n2+5 = 2(2)2+5 = 13
g(n) = 7(n) = 7(2) = 14
Here, f(n)<g(n)
Thus, for n=1, we get f(n) ≥ c∗ g(n).
This concludes that Omega helps to determine the
"lower bound" of the algorithm's run-time.
OR
16. Devise an algorithm for Quick sort and derive its time 10 3 2 4 4.4.3
B complexity. For the above algorithm find the time
complexity if all the elements are arranged in
ascending order. Illustrate with the help of recurrence
tree.
Quicksort is based on the three-step process of divide-
and-conquer.
• To sort the subarrayA[p . . r ]:
Divide: Partition A[p . . r ], into two subarrays A[p . .
q − 1] and A[q + 1 . . r ], such that each element in the
firstsubarray A[p . . q − 1] is ≤ A[q] and A[q] is ≤ each
element in the second subarrayA[q + 1 . . r ].
Conquer: Sort the two subarrays by recursive calls to
QUICKSORT.
Combine: No work is needed to combine the
subarrays, because they are sorted in place.
• Perform the divide step by a procedure
PARTITION, which returns the index q that marks the
position separating the subarrays.
QUICKSORT (A, p, r)
If p < r
then q ←PARTITION(A, p, r )
QUICKSORT (A, p, q − 1)
QUICKSORT (A, q + 1, r)
}
Swap(A[i + 1] ↔ A[r ] )
return i + 1
Complexity Analysis:
T(n) = O(nlogn)
Course Outcome (CO) and Bloom’s Level (BL) Coverage in the Questions
BL COVERAGE %
CO Coverage Percentage BL4
52 1%
BL1
51 33%
BL3
50 43%
Percentage
49
48 BL2
CO1 CO2 23%