Running Times Solution



Instructions: Please enter your answers into a single text file, called hw3.txt. Make sure it is clear which answer is to which question. Copy this file into the hw3 directory of your shared submission directories. Remember that flex days do not apply to homework assignments.

Question #1: Asymptotic Growth Rate

(R-4.13, page 186 of the textbook)

Order the following functions by their asymptotic growth rate:

#1: 4n log n + 2 n #4: 210 #7: 2 log n

#2: 3n + 100 log n #5: 4n #8: 2n

#3: n2 + 10n #6: n3 #9: n log n

Note that each function is numbered, from 1 to 9; write your answer as a sequence of index numbers, indicating the order or the functions in the table from smallest to largest O() function. So, you are stating functions listed earlier in the list have a smaller asymptotic growth rate than the functions you list later in the list.

Question #2: Runtime Analysis of Code

For each of the following code fragments, your task is to estimate its running time, in terms of n. You should assume that the value of n is defined elsewhere in the program. The running time that you report should be the tightest upper bound you can find. Don’t just say everything is O(2n), because although correct, it is not precise enough. Upper bounds that are too big will receive deductions during grading.

For each code sample:

State an upper bound on the running time of the program fragment using O( ) notation.

Briefly justify your upper bound.

Since you will be submitting a plain text file, format your answer by typing “n^2” for n2

Fragment A:

t = n ;

while ( t > 1 ) {

t = t /2 ;


Fragment B:

int something = 0 ;

for (int i=0 ; i < n ; i++) {

t = i ;

while ( t > 1 ) {

t = t / 2 ;



Fragment C:

int something = 0 ;

int t = n ;

while ( t > 1 ) {

for (int i=0 ; i < t ; i++) {

something++ ;


t = t / 2 ;


Fragment D:

t = n + 5 ;

while ( t < n) { // less than! (this is a trick question)

t = t + 5 ;


Fragment E:

int sum = 0;

int i = 1;

while (sum <= n) {

sum = sum + i;



Question #3

(adapted from R-4.29, page 188 of the textbook)

The definition of Big-Oh given in the textbook is:

Let f(n) and g(n) be functions mapping nonnegative integers to real numbers. We say that f(n) is O(g(n)) if there is a real constant c > 0 and an integer constant n0 1 such that for all n n0, f(n) c g(n).

Show that (n+1)5 is O(n5) by specifying the constants c and n0 in the definition above and providing the algebra that shows that the definition is satisfied.