CS200 Fall 2008 HW 2 due 9/30/08 at 3:00PM
1. Give a big-O estimate for each of these functions. For the function g in your estimate
f(x) is O(g), use a simple function g of smallest order.
a.
4. What is the order of each of the following tasks in the worst case? in the average case?
in the best case? For each, state the conditions that make it worst, average or best case.
a. Printing all n integers in a sorted linked list.
b. Searching an array of strings for a particular one using linear search.
c. Reading one line at a time from a file, where each line is a set of tab-delimited
integers; putting each number in each set into an array, sorting it using
bubblesort and then printing the array.[count the operations that move data]
d. Adding an item to a stack that is implemented using an ArrayList.
5. Given the following algorithm, what is its Big-O? Justify your answer using the three
theorems presented in the Complexity notes (and found in section 3.2 of Rosen).
Assume the line “d” takes O(1) or O(c).
a. for (int pass = 1; pass <= n; ++pass) {
b. for (int index = 0; index < n; ++index) {
c. for (int count = 1; count < 10; ++count) {
d. sum = sum + aArray[pass] + bArray[index] + cArray[count];
e. }
f. }
g. }