Question

In: Computer Science

Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested...

  1. Be able to determine the runtime complexity of:
  1. A Java assignment statement
  2. A single loop
  3. Nested loop

  1. Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).              Show that:            an expression such as the following                                                              

4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.     

  1. Which of the following statements are true?                                             
  1. The running time of a loop is, at most, the running time of the statements inside the loop times the number of iterations.  
  2. Consider the Java statement:

            int x = 250;

This statement has time complexity of T(n) = O(1).                           

  1. Consider the Java statement:

            int x = 250;

This statement has time complexity of T(n) = O(n).

Solutions

Expert Solution

Be able to determine the runtime complexity of:

1. A Java assignment statement :

Run time complexity is O(1)

2. A single loop

Run time complexity = no of iterations * complexity of statements inside loop

3.Nested loop

As said earlier for a single loop. Iteratively apply the same.

For k loops,

Run time complexity = no of iterations in loop1 * no of iterations in loop2 *.... No of iterations in loopk * complexity of statements inside loop1*complexity of statements inside loop2*.....*complexity of statements inside loopk

4. Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).

Show that: an expression such as the following 4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.

To find the witness we have run loop. As the highest degree of the polynomial is 3 here. We have to run 3 iterations of size n inorder to determine the c and k.

So, Run-Time complexity = O(x^3)    

5.Which of the following statements are true?         

                                    

  1. The running time of a loop is, at most, the running time of the statements inside the loop times the number of iterations.  
  2. Consider the Java statement:

            int x = 250;

This statement has time complexity of T(n) = O(1).                           

     3. Consider the Java statement:

            int x = 250;

This statement has time complexity of T(n) = O(n).

Among the statements the Statement 1 and 2 are correct. For example see the example of the loop given below.

for ( i =0 ; i < n ; i++){

// … Statements of 0(1)

}

Complexity of this loop is = O(n) = no of iterations * complexity of statements

= O(n) * O(1)

As we know that assignment statement complexity is O(1) not O(n).




Related Solutions

Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested...
Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested loop Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).  Show that: an expression such as the following                                                               4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.       Which of the following statements are true?                                              The running time of a loop is, at most, the running time of...
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ...
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ College, the student must meet the following requirements: GPA must be 3.75 or higher. Family income must be more than $60,000 Applicant must be a New Jersey resident. XYZ college asked you to write a program to implement the above requirements. Write the program using compound logic (AND, OR) whichever is applicable. Modify the program using nested if statement. Submit: Source code for both...
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ...
Java Programming Compound Logic and nested if statement For a student to be accepted in XYZ College, the student must meet the following requirements: GPA must be 3.75 or higher. Family income must be more than $60,000 Applicant must be a New Jersey resident. XYZ college asked you to write a program to implement the above requirements. Write the program using compound logic (AND, OR) whichever is applicable. Modify the program using nested if statement. This means there will be...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed When writing your CSV file, the first row (header row)...
Write a Java loop statement that prints count. Count begins with 1. It increments as multiples...
Write a Java loop statement that prints count. Count begins with 1. It increments as multiples of 2 and does the loop until count is less than 50.
java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1....
java programming Concepts ArrayList - Collections Sorting Enhanced For Loop Collections Class Auto-boxing Programming Assignment 1. Describe auto-boxing, including why it is useful. (Google for this one) Write a few lines of code that auto-box an int into an Integer, and un-box an Integer to an int. 2. Declare an ArrayList of Strings. Add 5 names to the collection. "Bob" "Susan" ... Output the Strings onto the console using the enhanced for loop. 3. Sort the list using the method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT