Question

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 total;
}


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;
}


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;
}

}

Actual big o representation

Ex1: Loop runs from 0 to n, hence linear O(N)
Ex2: Loop runs from 0 to n for n/2 times; but as we ignore constant terms, hence linear O(N)
Ex3:
For j = 0, inner loop runs 1 times.
For j = 1, inner loop runs 2 times.
..
For j = n, inner loop runs n times.

If we sum this, it comes to be n(n+1)/2, which is O(N^2) after ignoring constant terms.

Ex4:
The loop runs n times only. Hence O(N).

Ex5:
The 2 inner loops are same as Ex3, Hence their complexity is O(N^2), and the outer loop runs these inner loop N times, Hence total complexity becomes O(N^3)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
}

Answer: O(n)

Explanation: Here, for loop is executed for n times for each value of j.

Hence, the time complexity is n + k (where k is a positive constant)

Hence, for c = 2 and n0 = 1,

0 <= n + k <= 2n => Time complexity = O(n)


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;
}

Answer: O(n)

Explanation: Here, the possible values for j are: 0, 2, 4, ...., n. Here, the for loop is executed n/2 times.

Hence, the time complexity is n/2 + k (where k is a positive constant)

Hence, for c = 1 and n0 = 1,

0 <= n/2 + k <= n => Time complexity = O(n)


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;
}

Answer: O(n2)

Explanation:

The number of times the for loop is executed is given as:

0 + 1 + 2 + 3 + .... + n-1 (For each value of j] = n(n-1)/2 = (n2 - n)/2

Hence, the time complexity is (n2 - n)/2 + k (where k is a positive constant)

Hence, for c = 1 and n0 = 1,

0 <= (n2 - n)/2 + k <= n2 => Time complexity = O(n2)


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;
}

Answer: O(n)

Explanation: Here, the possible values for j are: 0, 1, 2, ...., n. Here, the for loop is executed n times.

Hence, the time complexity is n + k (where k is a positive constant)

Hence, for c = 2 and n0 = 1,

0 <= n + k <= 2n => Time complexity = O(n)

NOTE: As per Chegg policy, I am allowed to answer only 4 questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...
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
  • 8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n...

    8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n + 100logn      4n     2^n n^2 + 10n        n^3       nlogn 9.         R-4.9 Give a big-Oh characterization, in terms of n, of the running time of the example 1 method shown in Code Fragment 4.12. 10.       R-4.10 Give a big-Oh characterization, in terms of n, of the running time of the example 2 method shown in Code Fragment 4.12. 11.       R-4.11 Give a big-Oh characterization, in...

  • Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new...

    Question 9 0 Consider the following method. public static String[] strArrMethod(String] arr) String[] result = new String(arr.length]; for (int j - 0; j < arr.length; j++) String sm = arr[i]; for (int k = j + 1; k < arr.length; k++) if (arr[k].length() < sm.length) sm - arr[k]; // Line 12 result[j] = SM; return result; Consider the following code segment. String[] test Two - {"last", "day" of "the", school","year"); String[] resultTrostrar Method(test Two) How many times is the line...

  • 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)

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • 1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ)....

    1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...

  • JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....

    JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: ';' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: <identifier> expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:94: error: illegal start of type return true; ^ Project4.java:98: error: class, interface, or enum expected static int bSearch(int[] a, int count, int key) ^ Project4.java:101: error: class, interface, or...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...

    I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...

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