Professional Documents
Culture Documents
4n is also O(n) and larger than 3n+100logn because the 4n term is larger than the 3n term
nlogn is O(nlogn)
4nlogn+2n is also O(nlogn) because the nlogn term dominates the 2n term
210+10n is O(n2)
/∗∗ Returns the sum of the integers with even index in given array. ∗/
public static int example2(int[ ] arr) {
int n = arr.length, total = 0;
for (int j=0; j < n; j += 2)
total += arr[j];
return total;
}
Initializing variables n and total uses O(1) time.
-Initializing the array a uses O(n) time.
(using sum of aritmetic progression and big Oh definition, c=10, g(n) = n^2, n0=1)
g(n) = n^2
c1 = 1, c2=2, n0=1
T(n) =Theta(n^2)
.
Q5. Give a big-Oh characterization, in terms of n, of the running time of
the method shown in Code Fragment [1 Mark]
/∗∗ Returns the number of times second array stores sum of prefix sums
from first. ∗/
Important Instructions: