Dsa Q6 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ANALYSIS OF ALGORITHMS

(SET: 1)
Q.1 The running time of an algorithm T(n), where ‘n’ is the input size, is given by—
n
T(n) = 8 ⌈( ) + qn, if n > 1⌉ = p, if n = 1
2
where p, q are constants. The order of this algorithm is—

(a) n2 (b) nn
(c) n3 (d) n

Q.2 An algorithm is made up of 2 modules M1 and M2. If order of M1 is f(n) and M2 is g(n) then
the order of the algorithm is—

(a) max(f(n), g(n)) (b) min(f(n), g(n))


(c) f(n) + g(n) (d) f(n) * g(n)

Q.3 The concept of order (Big O) is important because—

(a) it can be used to decide the best algorithm that solves a given problem
(b) it determines the maximum size of a problem that can be solved in a given system, in a given
amount of time
(c) it is the lower bound of the growth rate of the algorithm
(d) Both (a) and (b)

Q.4 The running time T(n), where ‘n’ is the input size, of a recursive algorithm is given as
follows—
T(n)= C+T(n-1), if n>1
= d, if n≤ 1

The order of the algorithm is—


(a) n2 (b) n
(c) n3 (d) nn

Q.5 There are 4 different algorithms. A1, A2, A3, A4 to solve a given problem with the order
log(n), log(log(n)), nlog(n), n/log(n) respectively. Which is the best algorithm?

1
(a) A1 (b) A2
(c) A4 (d) A3

Q.6 The time complexity of an algorithm T(n), where n is the input size, is given by—
T(n)= T(n-1) + 1/n, if n>1
= 1, otherwise.

The order of the algorithm is—


(a) log n (b) n
(c) n2 (d) nn

Q.7 The running time of an algorithm is given by,


T(n)= T(n-1) + T(n-2) – T(n-3), if n>3
= n, otherwise.

The order of this algorithm is—


(a) n (b) log n
(c) nn (d) n2

Q.8 What should be the relation between T(1), T(2) and T(3), so that Q.7, gives an algorithm
whose order is constant?

(a) T(1)= T(2) = T(3) (b) T(1) + T(3)= T(2)


(c) T(1) – T(3)= T(2) (d) T(1) + T(2)= T(3)

Q.9 The order of a binary search algorithm is—

(a) n (b) n2
(c) nlog(n) (d) log(n)

Q.10 The recurrence relation that arises in relation with the complexity of binary search, ‘O’—

(a) T(n)= T(n/2) + k, where k is a constant


(b) T(n)= 2T(n/2) + k, where k is a constant
(c) T(n)= T(n/2) + log(n)
(d) T(n)= T(n/2) + n

2
Q.11 Consider the following 2 functions:

f(n)= n3, if 0 ≤ n < 10,000


= n2, otherwise
g(n)= n, if 0 ≤ n < 100
= n2 + 5n, otherwise

Which of the following option is correct?


(a) f(n) is O(n3) (b) g(n) is O(n3)
(c) O(f(n)) is same as O(g(n)) (d) g(n) is O(1)

Q.12 Let T(n) be the function defined by—

T(1)= 1, if n=1
n
= 2T (⌊2⌋) + √n, for n ≥ 2

Which of the following is true?


(a) T(n)= O(√n) (b) T(n)= O(n)
(c) T(n)= O(log n) (d) None of the above

Q.13 In the following function, let n ≥ m.

int gcd(n, m)
{ if(n%m2=0) return m;
n= n%m;
return gcd(m, n);
}

How many recursive calls are made by this function?


(a) Ѳ(log2n) (b) Ω(n)
(c) Ѳ(log2 log2 n) (d) Ѳ(√n)

Q.14 What is the time complexity of the following recursive function?

int Dosomething (int n) {


if(n≤2)
return 1;
else
return (Dosomething (floor(sqrt(n))) + n);

3
}

(a) Ѳ(n2) (b) Ѳ(nlog2n)


(c) Ѳ(log2n) (d) Ѳ(log2 log2 n)

Q.15 An array of n numbers is given, where n is an even number. The maximum as well as the
minimum of these n numbers needs to be determined. Which of the following is TRUE about the
no. of comparisons needed?

(a) Atleast 2n-C comparisons are needed


(b) Atmost 1.5n-2 comparisons are needed
(c) Atleast nlog2 n comparisons are needed
(d) None of the above

Q.16 Consider the following C code segment:

int IsPrime (n)


{
int i, n;
for (i=2; I ≤ √n; i++)
{
if(n% i= =0)
{
printf(“Not prime\n”);
return 0;
}
return 1;
}
}

Let T(n) denote the no. of times the ‘for’ loop is executed by the program on input n. Which of
the following is TRUE?

(a) T(n)= O(√n) and T(n)= Ω(√n)


(b) T(n)= O(√n) and T(n)= Ω(1)
(c) T(n)= O(n) and T(n)= Ω(√n)
(d) None of the above

4
Q.17 The minimum no. of comparisons required to determine if an integer appears more than n/2
times in a sorted array of n integers.

(a) Ѳ(n) (b) Ѳ(log n)


(c) Ѳ(log logn) (d) Ѳ(1)

The next two questions(Q.18 & Q.19) are based on the following:

int f1 (int n)
{
if (n= =0 || n= =1)
return n;
else
return (2*f1 (n-1) + 3*f1 (n-2));
}
int f2 (int n)
{
int i;
int X[N], Y[N], Z[N];
X[0]=Y[0]=Z[0]=0;
X[1]=1; Y[1]=2; Z[1]=3;
for (i=2, i≤ n, i++) {
X[i]= Y[i-1] + Z[i-2];
Y[i]= 2 * X[i];
Z[i] = 3 * X[i];
}
return X[n];

Q.18 The running time of f1(n) and f2(n) are—

(a) Ѳ(n) and Ѳ(n) (b) Ѳ(2n) and Ѳ(n)


(c) Ѳ(n) and Ѳ(2n) (d) Ѳ(2n) and Ѳ(2n)

Q.19 f1(8) and f2(8) returns the value of—

(a) 1661 and 1640 (b) 59 and 59


(c) 1640 and 1640 (d) 1640 and 1641

5
Q.20 The running time of the algorithm is represented by following recurrence relation:
T(n)= n, if n≤ 3
T(n/3) + Cn, otherwise

Which is the time complexity?


(a) Ѳ(n) (b) Ѳ(nlogn)
(c) Ѳ(n2) (d) Ѳ(n2logn)

You might also like