O-notation

advertisement
CS 315 Homework #2,
1. What is the O-notation for the following functions [1 point each]:
(a) f(n) = 400
(b) f(n) = 400 + .001n
(c)
f(n) = 400 + 10n +.02n2
(d)
f(n) = 400 + 200nlg(n) +.02n2
(e) f(n) = 400 + 200nlg(n) +.02n2lg(n)
(f) f(n) = 200*n5 + 0.01*2n
2. True or false: An algorithm that is O(n2) is always faster than one that is O(n3).
Justify your answer briefly. [4 points]
3. What is the O-notation (as a function of n) for the following code fragments:
(a) [2 points]
For (I=0; I<n; I++)
For (J=0; J<n; J++)
X = 0;
(b) [2 points]
For (I=0; I<n; I++)
For (J=0; J<(pow(n,3)+pow(n,2)); J++)
X = 0;
(c) [2 points]
For (I=0; I<n; I++)
For (J=n-7; J<n+3; J++)
X = 0;
(d) [4 points]
Z = 13;
For (I=0; I<pow(n,3); I+=200)
For (J=n-7; J<n+3; J++)
X = 0;
For (I=0; I<pow(n,2); I+=2)
Y = 1;
/* pow(X,Y) is X to the Yth power in C */
4.This one is optional. Answer it and provide a mathematically acceptable proof
of your answer and I'll excuse you from the 1st midterm with an automatic 'A':
for (i=1; i<n; i++)
{
for (j=0; j<n; j+=i )
X = 0;
}
Download