- Express the following running times in big O:
- O(n)
- O(n2)
- O(1)
- O(n2)
- O(n2)
- O(n)
- O(n log n)
- O(n2)
- O(n2)
-
Consider the following pseudocode:
count = 0;
for (i = 1 ; i <= N ; i++)
count++;
System.out.println(count);
-
Statement |
Worst |
(1) |
1 |
(2) |
N+1 |
(3) |
N |
(4) |
1 |
-
Statement |
Best |
(1) |
1 |
(2) |
N+1 |
(3) |
N |
(4) |
1 |
- 1 + N+1 + N + 1 = 2N + 3
- 1 + N+1 + N + 1 = 2N + 3
- O(N)
- O(N)
-
Consider the following pseudocode:
count = 0;
for (i = 0 ; i < 10 ; i++)
count++;
System.out.println(count);
-
Statement |
Worst |
(1) |
1 |
(2) |
11 |
(3) |
10 |
(4) |
1 |
-
Statement |
Best |
(1) |
1 |
(2) |
11 |
(3) |
10 |
(4) |
1 |
- 1 + 11 + 10 + 1 = 23
- 1 + 11 + 10 + 1 = 23
- O(1)
- O(1)
-
Consider the following pseudocode:
count = 0;
amount = 0;
for (i = 1 ; i <= N ; i++)
count++;
System.out.println(count);
for (i = 1 ; i < N ; i++)
arr[i] += count;
-
Statement |
Worst |
(1) |
1 |
(2) |
1 |
(3) |
N+1 |
(4) |
N |
(5) |
1 |
(6) |
N |
(7) |
N-1 |
-
Statement |
Worst |
(1) |
1 |
(2) |
1 |
(3) |
N+1 |
(4) |
N |
(5) |
1 |
(6) |
N |
(7) |
N-1 |
- 1 + 1 + N+1 + N + 1 + N + N-1 = 4N + 3
- 1 + 1 + N+1 + N + 1 + N + N-1 = 4N + 3
- O(N)
- O(N)
-
Consider the following pseudocode:
num = 1;
amount = N;
amount = amount + N * (N - 1);
num = num + amount;
-
Statement |
Worst |
(1) |
1 |
(2) |
1 |
(3) |
1 |
(4) |
1 |
-
Statement |
Worst |
(1) |
1 |
(2) |
1 |
(3) |
1 |
(4) |
1 |
- 1 + 1 + 1 + 1 = 4
- 1 + 1 + 1 + 1 = 4
- O(1)
- O(1)
-
Consider the following pseudocode:
(1) tot = 0
(2) for (i = 0 ; i < n ; i++) {
(3) C[i] = A[i] + B[i]
(4) if (A[i] > B[i])
(5) tot = tot + A[i]
}
-
Statement |
Worst Case Count |
Worst Case Array Refs |
(1) |
1 |
0 |
(2) |
n+1 |
0 |
(3) |
n |
3n (3 array references per statement) |
(4) |
n |
2n (2 array references per statement) |
(5) |
n |
n (1 array reference per statement) |
-
Statement |
Best Case Count |
Best Case Array Refs |
(1) |
1 |
0 |
(2) |
n+1 |
0 |
(3) |
n |
3n (3 array references per statement) |
(4) |
n |
2n (2 array references per statement) |
(5) |
0 |
0 |
- In the worst case the total number of statements executed is
1 + n+1 + n + n + n = 4n + 2 which is O(n).
- In the best case the total number of statements executed is
1 + n+1 + n + n = 3n + 2 which is O(n).
- The total number of array accesses in the worst case
is 6n. The algorithm is O(n).
-
Consider the following pseudocode:
tot = 0
if ((A[0] == B[0])
for (i = 1 ; i < n ; i++) {
C[i] = A[i] + B[i]
tot = tot + A[i]
}
-
Statement |
Worst Case Count |
Worst Case Array Refs |
(1) |
1 |
0 |
(2) |
1 |
2 |
(3) |
n |
0 |
(4) |
n-1 |
3(n-1) (3 array references per statement) |
(5) |
n-1 |
n-1 (1 array reference per statement) |
-
Statement |
Best Case Count |
Best Case Array Refs |
(1) |
1 |
0 |
(2) |
1 |
2 |
(3) |
0 |
0 |
(4) |
0 |
0 |
(5) |
0 |
0 |
- In the worst case the total number of statements executed is
1 + 1 + n + n-1 + n-1 = 3n which is O(n).
- In the best case the total number of statements executed is
1 + 1 + 0 + 0 + 0 = 2 which is O(1).
- The total number of array accesses in the worst case
is 2 + 3(n-1) + n-1 = 4n-2.
The algorithm is O(n).
-
Consider the following pseudocode:
count = 0;
for (i = 1 ; i <= N ; i++) {
count++;
for (k = 1 ; k <= N/4 ; k++)
arr[k] += count;
}
System.out.println(count);
-
Statement |
Worst |
(1) |
1 |
(2) |
N+1 |
(3) |
N |
(4) |
N(N/4+1) |
(5) |
N(N/4) |
(6) |
1 |
Statement |
Worst |
(1) |
1 |
(2) |
N+1 |
(3) |
N |
(4) |
N(N/4+1) |
(5) |
N(N/4) |
(6) |
1 |
- 1 + N+1 + N + N(N/4+1) + N(N/4) + 1 = 1 + N+1 + N + N2/4+N + N2/4 + 1 = N2/2 + 3N + 3
- 1 + N+1 + N + N(N/4+1) + N(N/4) + 1 = 1 + N+1 + N + N2/4+N + N2/4 + 1 = N2/2 + 3N + 3
- O(N2)
- O(N2)