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 Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
1.If one statement is nested within another statement, as in a loop or recursive call, what...
1.If one statement is nested within another statement, as in a loop or recursive call, what is the running time of the two statements when considered as a block? the running time for the inner statement added to the running time for the outer statement the running time for the inner statement multiplied by the running time for the outer statement the running time for the inner statement divided by the running time for the outer statement the running time...
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 Introduction: Runtime complexity is an issue when determining which algorithms we should use. Some algorithms...
java Introduction: Runtime complexity is an issue when determining which algorithms we should use. Some algorithms are easy to implement but run too slowly to be useful. Knowing the advantages and disadvantages of different algorithms helps in determining the best solution to a problem. Description: You are to implement two different sorting algorithms and count the number of steps involved with the sort routine (the comparisons and moving positions). Every time a comparison or move is made you should count...
JAVA Write nested while loop that will print out this pattern, based upon a number entered...
JAVA Write nested while loop that will print out this pattern, based upon a number entered by the user. User enters 4: 1234 1234 1234 1234 User enters 2: 12 12
Write a java program: Write a nested loop program creating an array list of three positions.  The...
Write a java program: Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT