- Express the following running times in big O:
- 10n
- 10n + 2n2
- 6
- 5n2 + n
- n2 + 5n
- logn + 5n + 3
- n + 2nlogn
- n * (n - 1)) / 2
- 2n2 + 5nlogn + 1
-
Consider the following pseudocode:
count = 0;
for (i = 1 ; i <= N ; i++)
count++;
System.out.println(count);
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed in the worst case?
- What is the total number of statements executed in the best case?
- What is the big O in the worst case?
- What is the big O in the best case?
-
Consider the following pseudocode:
count = 0;
for (i = 0 ; i < 10 ; i++)
count++;
System.out.println(count);
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed in the worst case?
- What is the total number of statements executed in the best case?
- What is the big O in the worst case?
- What is the big O in the best case?
-
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;
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed in the worst case?
- What is the total number of statements executed in the best case?
- What is the big O in the worst case?
- What is the big O in the best case?
-
Consider the following pseudocode:
num = 1;
amount = N;
amount = amount + N * (N - 1);
num = num + amount;
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed in the worst case?
- What is the total number of statements executed in the best case?
- What is the big O in the worst case?
- What is the big O in the best case?
-
Consider the following pseudocode:
tot = 0
for (i = 0 ; i < n ; i++) {
C[i] = A[i] + B[i]
if (A[i] > B[i])
tot = tot + A[i]
}
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed and what
is the big O in the worst case?
- What is the total number of statements executed and what
is the big O in the best case?
- Sometimes we want to focus on certain operations, and ignore
"bookkeeping" operations like testing loop conditions and incrementing
subscripts. For example, we might want to count the number of times
we use an array. In that case we count array references: any time
we use an array in an assignment statement or a condition.
We count each time
we use an array, even if we use it more than once in a statement.
Count the total number of references to
array elements in the worst case. Using the count of array references,
what is the big O complexity of this
algorithm in the worst case?
-
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]
}
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed and what
is the big O in the worst case?
- What is the total number of statements executed and what
is the big O in the best case?
- Sometimes we want to focus on certain operations, and ignore
"bookkeeping" operations like testing loop conditions and incrementing
subscripts. For example, we might want to count the number of times
we use an array. In that case we count array references: any time
we use an array in an assignment statement or a condition.
We count each time
we use an array, even if we use it more than once in a statement.
Count the total number of references to
array elements in the worst case. Using the count of array references,
what is the big O complexity of this
algorithm in the worst case?
-
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);
- In the worst case, how many times is each statement executed?
- In the best case, how many times is each statement executed?
- What is the total number of statements executed in the worst case?
- What is the total number of statements executed in the best case?
- What is the big O in the worst case?
- What is the big O in the best case?
-
Consider the following pseudocode:
tot = 0;
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
if (arr[i][j] > 0) {
tot = tot + arr[i][j];
arr[i][j] = 0;
}
}
}
- In the worst case, how many times is each statement executed?
- Count the total number of references to
array elements in the worst case. If the operation we
are counting is array references, what is the big O complexity of this
algorithm in the worst case?
- Count the total number of references to
array elements in the best case. If the operation we
are counting is array references, what is the big O complexity of this
algorithm in the best case?
-
Consider the following pseudocode:
count = 0;
for (int i = 0 ; i < N ; i++)
for (int j = 0 ; j < N ; j++)
if (a[i] + a[j] + a[k] == 0)
for (int k = 0 ; k < N ; k++)
count++;
- In the worst case, how many times is each statement executed?
- What is the big O complexity in the worst case?
- In the best case, how many times is each statement executed?
- What is the big O complexity in the best case?