Question: 1 . Give a big - Oh characterization, in terms of n , of the running time of the example 1 method from Exercises.java class

1.Give a big-Oh characterization, in terms of n, of the running time of the example1 method from Exercises.java class in Lesson4Examples folder.
2.Give a big-Oh characterization, in terms of n, of the running time of the example2 method from Exercises.java class in Lesson4Examples folder.
3.Give a big-Oh characterization, in terms of n, of the running time of the example3 method from Exercises.java class in Lesson4Examples folder.
4.Give a big-Oh characterization, in terms of n, of the running time of the example 4 method from Exercises.java class in Lesson4Examples folder.
5.Give a big-Oh characterization, in terms of n, of the running time of the example 5 method from Exercises.java class in Lesson4Examples folder.
For each of the above questions, use comments in the code to provide the results and a brief explanation.
Heres the code:
class Exercises {
/** Returns the sum of the integers in given array. */
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;
}
/** 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)// note the increment of 2
total += arr[j];
return total;
}
/** Returns the sum of the prefix sums of given array. */
public static int example3(int[] arr){
int n = arr.length, total =0;
for (int j=0; j < n; j++)// loop from 0 to n-1
for (int k=0; k <= j; k++)// loop from 0 to j
total += arr[j];
return total;
}
/** Returns the sum of the prefix sums of given array. */
public static int example4(int[] arr){
int n = arr.length, prefix =0, total =0;
for (int j=0; j < n; j++){// loop from 0 to n-1
prefix += arr[j];
total += prefix;
}
return total;
}
/** Returns the number of times second array stores sum of prefix sums from first. */
public static int example5(int[] first, int[] second){// assume equal-length arrays
int n = first.length, count =0;
for (int i=0; i < n; i++){// loop from 0 to n-1
int total =0;
for (int j=0; j < n; j++)// loop from 0 to n-1
for (int k=0; k <= j; k++)// loop from 0 to j
total += first[k];
if (second[i]== total) count++;
}
return count;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!