Comparing Run Times

Help Questions

AP Computer Science A › Comparing Run Times

Questions 1 - 10
1

Consider the following code:

O_n_

What is the expected runtime of the code (in Big-O notation?)

Explanation

The run-time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed n times in the for loop, leading to a run time that is proportional to n, or O(n).

2

Consider the following code:

O_n_

What is the expected runtime of the code (in Big-O notation?)

Explanation

The run-time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed n times in the for loop, leading to a run time that is proportional to n, or O(n).

3

Consider the following code:

O_n_

What is the expected runtime of the code (in Big-O notation?)

Explanation

The run-time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed n times in the for loop, leading to a run time that is proportional to n, or O(n).

4

Consider the following code:

O_2n_

What is the run time of the code (in Big-O notation?)

Explanation

The run time of this code is best analyzed by plugging in numbers. Take the value of bar(2) for example. bar(2) leads to two calls to bar(1), which lead to four calls to bar(0). A call to bar(4) leads to two calls to bar(3), four calls to bar(2), eight calls to bar(1), and sixteen calls to bar(0). Thus, it can be shown that the number of calls is proportional to 2^n, and that that run time is O(2^n).

5

Consider the following code:

O_2n_

What is the run time of the code (in Big-O notation?)

Explanation

The run time of this code is best analyzed by plugging in numbers. Take the value of bar(2) for example. bar(2) leads to two calls to bar(1), which lead to four calls to bar(0). A call to bar(4) leads to two calls to bar(3), four calls to bar(2), eight calls to bar(1), and sixteen calls to bar(0). Thus, it can be shown that the number of calls is proportional to 2^n, and that that run time is O(2^n).

6

Consider the following code:

O_2n_

What is the run time of the code (in Big-O notation?)

Explanation

The run time of this code is best analyzed by plugging in numbers. Take the value of bar(2) for example. bar(2) leads to two calls to bar(1), which lead to four calls to bar(0). A call to bar(4) leads to two calls to bar(3), four calls to bar(2), eight calls to bar(1), and sixteen calls to bar(0). Thus, it can be shown that the number of calls is proportional to 2^n, and that that run time is O(2^n).

7

Consider the following code:

O_n3_

What is the run time of the code (in Big-O notation)?

Explanation

The run time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed times in the inner "for" loop, and times in the outer "for" loop. Thus, the overall run time is .

8

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

for( int j = 1; j < n; j *= 2){

someFunction();

}

}

For the code above, what is the run time in Big O notation?

O(n log(n) )

O( )

O( log(n) )

O( n )

None of the above

Explanation

At first glance we might be tempted to pick O( ) because there are 2 for loops. But, upon closer inspection we can see that the first loop will yield a O( n ) running time but the second loop does not. The second loop has only an O( log(n) ) running time because "j" doubles each iteration and does not increase linearly. That will yield O( log(n) ) since O( log(n) ) is a much faster running time. So the final result is O( n log(n) ).

9

Consider the following code:

O_n3_

What is the run time of the code (in Big-O notation)?

Explanation

The run time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed times in the inner "for" loop, and times in the outer "for" loop. Thus, the overall run time is .

10

Consider the following code:

O_n3_

What is the run time of the code (in Big-O notation)?

Explanation

The run time can best be analyzed by breaking the code down by line. The line iterating the value of sum is performed in constant time, or O(1). This constant time operation is performed times in the inner "for" loop, and times in the outer "for" loop. Thus, the overall run time is .

Page 1 of 4
Return to subject