Question

Big-O notation for eachpublic static double accumulate (double[] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) sum += a[i]; return sum;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOL:

1) The time complexity is O(a.length)

if we consider a.length =n

Then Big-O is O(n)

Explanation:

The Loop runs from O to a.length . so the no of iterations will be a.length

The time complexity is directly propotional to number of iterations

No of iterations will be a.length

if we consider a.length = n

So Big-O is O(n)

2) The time complexity is O(a.length)

if we consider a.length =n

Then Big-O is O(n)

Explanation:

The Loop runs from O to a.length .so the no of iterations will be a.length

The time complexity is directly propotional to number of iterations

No of iterations will be a.length

if we consider a.length = n

So Big-O is O(n)

3) The time complexity is O(n2) i.e Big-O is O(n2) if we consider a.length =n

Explanation:

The time complexity is directly propotional to number of iterations

For i=1 The no of iterations will be n-1 times

For i=2 The no of iterations will be n-2 times

for i=3 The no of iterations will be n-3 times

So the no of iterations will be

n-1+n-2+n-3+...+1

=n(n+1)/2

= n2+n/2

= O(n2)

4) The time complexity is O(a.length)

if we consider a.length =n

Then Big-O is O(n)

Explanation:

The Loop runs from O to a.length . so the no of iterations will be a.length

The time complexity is directly propotional to number of iterations

No of iterations will be a.length

if we consider a.length = n

So Big-O is O(n)

Add a comment
Know the answer?
Add Answer to:
Big-O notation for each public static double accumulate (double[] a) { double sum = 0.0; for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of this code segment? double [] a = {-1.2, 3.1, -4.7, 38.0,...

    What is the output of this code segment? double [] a = {-1.2, 3.1, -4.7, 38.0, 0.0}; double temp = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] < temp) { temp = a[i]; } } System.out.println (temp); What does this method do? In one short English sentence describe the task accomplished by this method. public static int foo(int [] a) { int temp = 0; for (int i = 0; i < a.length; i++)...

  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement...

    Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement a more efficient runtime algorithm for intervalSums. public static long intervalSums(intll A, intl0 B) long count = 0; for (int i = 0; i < A.|ength; i++) { for (int j = i; j < A.length; j++) { int sum = 0; for (int k = i; k <= j; k++) { sum += A[k]; count += 1; B[i][j] = sum; if (j >...

  • Compute the Big O notation. Explain how you got the answer. on W NA 1 public...

    Compute the Big O notation. Explain how you got the answer. on W NA 1 public String modify (String str) { if (str.length() <= 1) return ""; int half = str.length() / 2; modify(str.substring(half)); 5} 1 2 3 for (int i = 0; i<n; i++) { for (int j 0; j < 5; j++) { for (int k = 0; k<n; k++) { 4 if ((i != j) && (i != k)) { 5 System.out.println(k); 6 } 7 } 8...

  • public class StatsAnalyzer { public static int PlayerTotalPoints(int[] [] scores, int p) { // assuming the...

    public class StatsAnalyzer { public static int PlayerTotalPoints(int[] [] scores, int p) { // assuming the 1st player data is at index and 11 last player index is at p-1 index if (0 <= p && p < scores. length) { int totalPoints = 0; Il player p data will be in row scores (pl for (int game = 0; game < scores (p).length; game:+) { totalPoints += scores (p) (game); return totalPoints; ONECANNSBEREBRO VOLAWN return @; public static double...

  • Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...

    Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise { public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return...

  • What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...

    What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Big-O notation. Consider the following function. int func1(int n) { int sum = 0, i; for(i...

    Big-O notation. Consider the following function. int func1(int n) { int sum = 0, i; for(i = 0; i<n; i++;) { sum += i; return sum; } Express the running time of func1 as a function of n using big-O notation. Write a function that has the same functionality as func1, but runs in O(1) time.

  • 5. What is the Big Oh method m2? public static void m2(int[] arr, int n) for...

    5. What is the Big Oh method m2? public static void m2(int[] arr, int n) for (int í = 1; í <= n- 1; i++) pM2(arr [i], arr, 0, i - 1); // end m2 private static void pM2(int entry, int[l arr, int begin, int end) int i- end; for(; (i 〉= begin) && (entry 〈 arr [i]); i--) arr [1 + 1] = arr L1] arr[i + 1] - entry; return // end pM2

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT