Uploaded by yanghao.bowen

Tutorial01

advertisement
Data Structure
Tutorial 01
Prepared by Dr. Patrick Chan
1. Arrange the following expressions by growth rate from slowest to fastest:
4n2
Log3n
n!
3n
20n
2
log2n
n2/3
2. a) Suppose that a particular algorithm has time complexity T(n) = n2, and that
executing an implementation of it on a particular machine takes T seconds for n
inputs. Now suppose that we are presented with a machine that is 64 times as fast.
How many inputs could we process on the new machine in T seconds?
b) Suppose that a particular algorithm has time complexity T(n) = 8n, and that
executing an implementation of it on a particular machine takes T seconds for n
inputs. Now suppose that we are presented with a machine that is 64 times as fast.
How many inputs could we process on the new machine in T seconds?
3. Using the definitions of big-Oh and Ω, find the upper and lower bounds for the
following expressions. Be sure to state appropriate values for c and n0.
(a) c1 n
(b) c2 n3 + c3
(c) c4 n logn + c5 n
Data Structure – Tutorial 1
1
4. Determine i) the time complexity and ii) Θ for the following code fragments in the
average case. Assume that all variables are of type int and n is the user input.
a) a = a + c;
d = a + e;
b) sum = 0;
for (i=0; i<8; i++)
sum--;
c) a = 0;
while (a <= n)
a++;
d) sum = 0;
for (i=0; i<3; i++)
for (j=0; j<n; j++)
sum++;
e) for (i=0; i<n; i++) {
for (j=0; j<n; j++)
A[i] = Random(n); // assume random() is O(1)
sort(A, n); // assume sort() is O(n log n)
}
f) sum = 0;
for (i = 0; i < n; i++)
if (is_even(i))
for (j = 0; j < n; j++)
sum++;
else
sum = sum + n;
Data Structure – Tutorial 1
2
Download