Loyola College in Maryland

CS 301 - Data Structures and Algorithms I
Fall 2003


Loyola College > Department of Computer Science > CS 301 > Homework > Homework 6

Due

Friday, September 26 at the beginnging of class

Problems

1. How many comparisons of array items do the following loops contain?
for (j = 1; j <= n-1; ++j) {
i = j +1;
do {
if (theArray[i] < theArray[j])
swap(theArray[i], theArray[j]);
++i;
} while (i <= n);
} // end for
2. What order-of-magnitude is an algorithm that has as a growth-rate function
a. 8 * n3 - 9 * n
b. 7 * log2 n + 200
c. 7 * log2 n + n

3.Suppose that your implemenation of a particular algorithm appears in C++ as
for (int pass = 1; pass <= n; ++pass) {
for (int index = 0; index < n; ++index) {
for(int count = 1; count < 10; ++count) {
...
} // end for
} // end for
} //end for
The previous code shows only the repetition in the algorithm, not the computations that occur within the loops. These computations, however, are independent of n. What is the order of the algorithm? Justify your answer.

4. Show that for all constants a, b > 1, f(n) is O(loga n) if and only if f(n) is O(logb n). Thus, you can omit the base when you write O(log n). Hint: Use the identity loga n = logb n / logb a for all constants a,b > 1.
Prove: loga n = O(logb n) and logb n = O(loga n) for all constants a,b > 1.