Uploaded by Matthew Singh

Assignment 1

advertisement
Assignment 1
For each question, you are required to identify the number of the choice that
best completes the statement or answers the question.
For questions 1 to 15 give the algorithmic complexity of code fragment
provided:
Question 1
for(int i = 0; i < n; i++)
sum++;
1. O(1)
2. O(Log N)
3. O(N)
4. O(N log N)
Question 2
for(int i = 0; i < n; i += 2)
sum++;
1. O(N)
2. O(Log N)
3. O(1)
4. O(N log N)
Question 3
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
sum++;
1. O(1)
2. O(N2)
3. O(N3)
4. O(N log N)
Question 4
for(int i = 0; i < n; i++)
sum++;
for(int j = 0; j < n; j++)
sum++;
1. O(N2)
2. O(Log N)
3. O(N)
4. O(N log N)
Question 5
for(int i = 0; i < 6n; i += 3)
sum++;
1. O(3N)
3. O(N)
2. O(6N)
4. O(18N)
Question 6
for(int i = 0; i < n; i++)
for(int j = 0; j < n*n; j++)
sum++;
1. O(1)
2. O(N2)
3. O(N3)
4. O(N log N)
Question 7
for(int i = 0; i < n; i++)
for(int j = 0; j < i; j++)
sum++;
1. O(N log N)
2. O(N2)
3. O(N2 log N)
4. O(N3)
Question 8
for(int i = 0; i < n; i++)
for(int j = 0; j < n + 10; j++)
sum++;
1. O(N)
2. O(N2)
3. O(N3)
4. O(N log N)
Question 9
for(int i = 0; i < n*n; i++)
for(int j = 0; j < i; j++)
sum++;
1. O(N4)
2. O(N2)
3. O(N3)
4. O(N5)
Question 10
for(int i =0; i < n; i++)
for(int j = 0; j < n*n; j++)
for(int k = 0; k < j; k++)
sum++;
1. O(N4)
2. O(N2)
3. O(N3)
4. O(N5)
Question 11
for(int i = 1; i < n; i = i*2)
sum++;
1. O(1)
2. O(N2)
3. O(log N)
4. O(N log N)
Question 12
for(int i = 1; i < n; i = i*2)
sum++;
for(int i = 1; i < n; i = i*2)
sum++;
1. O(1)
2. O(N2)
3. O(log N)
4. O(N log N)
Question 13
for(int i = 0; i < 100000; i++)
for(int j = 0; j < i; j++)
sum++;
1. O(1)
2. O(N)
3. O(2n)
4. O(100000i)
Question 14
for(int i = 0; i < n; i++)
for(int j = n; j > 0; j= j/2)
sum++;
1. O(N )
2. O(N2)
Question 15
i=1
while(i<=n){
for(j=1; j<10; j++)
doIt();
i=i+1;}
3. O( log N )
4. O( N log N)
where doIt has a runtime equivalent of 5n
1. O(N log N)
2. O(N3)
Question 16
3. O(N2)
4. O(50N2)
An algorithm takes 10s for input size of 100. How long will it take for an input size of
10000 if the running time is log N.
1. 10 s
2. 20 s
3. 30 s
4. 40 s
Question 17
An algorithm takes 10 s for input size of 1000.How long will it take for an input size of
2000 if the running time is quadratic.
1. 10 s
2. 20 s
3. 30 s
4. 40 s
Question 18
An algorithm takes 10 s for input size of 1000. How large a problem can be solved in
80 s if the running time is cubic.
1. An input of size 800
2. An input of size 2000
3. An input of 1250
4. An input of size 30000
Question 19
Which of the following functions g(n) has the fastest growth rate (for n > 1)?
1. N
2. n2
Question 20
3. n log n
4. 1
Which of the following functions has the fastest growth rate (for n > 1)?
1. N
2. log n
3. n2 log n
4. n2
Question 21
Which of the following functions g(n) has the slowest growth rate (for n > 1)?
1. n log n
2. n2
3. log n
4. N
Question 22
Order the Big-Oh values of the following functions by growth rate from smallest to
largest:
n/1000 + 23; 1000/n + 23 ; 3 log n; n2 – 23n; 1023
1. O(n); O(1/n); O(log n); O(n2); O(1)
2. O(1); O(1/n); O(log n); O(n); O(n2)
3. O(1/n); O(1); O(log n); O(n); O(n2)
4. O(1/n); O(log n); O(1); O(n); O(n2)
Consider the table below and answer questions 23 to 25. The table depicts the
running time (seconds) dependant on input size (n) of three algorithms
Algorithm n = 100
A
B
C
4
9
21
n = 1000 n = 10
000
400
40 000
90
900
31
41
Question 23
What is the running time complexity of algorithm A?
1. O(n)
2. O(n2)
3. O(log n)
4. O(2n)
Question 24
What is the running time complexity of algorithm B?
1. O(n)
2. O(n2)
3. O(log n)
4. O(2n)
Question 25
What is the running time complexity of algorithm C?
1. O(1)
2. O(n)
3. O(log n)
4. O(1/n)
Download