Algorithm Analysis

Help Questions

AP Computer Science A › Algorithm Analysis

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_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).

5

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).

6

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).

7

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).

8

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).

9

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).

10

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).

Page 1 of 16