Question

Demonstrate in a small code snippet O(n^2) (n to the power of 2) Please code in...

Demonstrate in a small code snippet O(n^2) (n to the power of 2)

Please code in Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//TestCode.java
import java.util.Scanner;
public class TestCode {
  public static void main(String args[]){
    int n;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter value for n: ");
    n = scanner.nextInt();
    for(int i = 0;i<n;i++){
      for(int j = 0;j<n;j++){
        System.out.print("*");
      }
      System.out.println();
    }
  }
}

===================================

Each loop is running for n times.

So, time complexity = O(n*n)

Add a comment
Know the answer?
Add Answer to:
Demonstrate in a small code snippet O(n^2) (n to the power of 2) Please code in...
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
  • The following code snippet is for C++ int selection_Sort(int A[ ], int n) { int I,...

    The following code snippet is for C++ int selection_Sort(int A[ ], int n) { int I, j, small, temp; for( i = 0; i < n-1; i++) { small = i; for(j = i + 1; j < n; j++) { if ( A[ j ] < A[ small ] small = j; } temp = A [ i ]; A[ i ] = A[small]; A[small] = temp; } } Please explain in rich detail the logic behind every execution...

  • 1. Write a code snippet Write a code snippet that asks the user to enter a...

    1. Write a code snippet Write a code snippet that asks the user to enter a name and a major one at a time and creates a dictionary with keys corresponding to names and values corresponding to majors. Assume that the user enters “alice”, “bio”, “mark”, “engineering”. >>>d = {} >>> your code starts here 2.Based on your code above: >>> print(d) 3.Write a code snippet Write a code snippet that creates a dictionary (records) using keys from a frozenset...

  • Java Question Please highlight the 1 correct option from 4 given at bottom Which code snippets...

    Java Question Please highlight the 1 correct option from 4 given at bottom Which code snippets are valid return statements for method1() and method2()? public void method1(){   // CODE SNIPPET 1 } public Float method2(){   // CODE SNIPPET 2 } return null; for CODE SNIPPET 1 and return 12.3; for CODE SNIPPET 2 return; for CODE SNIPPET 1 and return null; for CODE SNIPPET 2 return null; for both CODE SNIPPET 1 and CODE SNIPPET 2 return; for CODE SNIPPET...

  • 1). What is the complexity of the following code snippet? { for (int count2 = 0;...

    1). What is the complexity of the following code snippet? { for (int count2 = 0; count2<n; count2++) { /*some sequence of O(1) step*/ } } select one: a. O(N^2) b. O(Log N) c. O(1) d. O(N!) 2). What is the complexity of the following code snippet? for (int count = 0; count<n; count++) { printsum(count) } select one: a. We need to know the complexity of the printsum() function. b. O(Log N) c. O(1) d. O(N) e. O(N^2) 3)....

  • What is the value of x after the execution of the code snippet. Give an exact...

    What is the value of x after the execution of the code snippet. Give an exact answer as a function of N. Justify your answer. x = 0 for J = 1 to N do x = x + 2 end for Using Theta notation, how many bits do you need to represent x after the execution of the code snippet?

  • programming in JAVA,,, Using 'while' loop write a snippet of code that would print on one...

    programming in JAVA,,, Using 'while' loop write a snippet of code that would print on one line all powers of 2 that are less than 5000 separated by spaces. Declare all variables that you might need.

  • find complexity Problem 1 Find out the computational complexity (Big-Oh notation) of the code snippet: Code...

    find complexity Problem 1 Find out the computational complexity (Big-Oh notation) of the code snippet: Code 1: for (int i = n; i > 0; i /= 2) { for (int j = 1; j < n; j *= 2) {     for (int k = 0; k < n; k += 2) {               // constant number of operations here     } } } Code 2: Hint: Lecture Note 5, Page 7-8 void f(int n) { if (n...

  • Given the following Java code snippet from a rental car management system class Car{ //code omitted...

    Given the following Java code snippet from a rental car management system class Car{ //code omitted public boolean pickPassenger(Passenger psg, int psgID) {           //code omitted           Passenger localPassenger = psg;           //code omitted          } } The pickPassenger() method implements code for picking up a Passenger object for a ride by a Car object. Given the Passenger object reference is local to the pickPassenger() method and is not required for any other method or field of class Car, the...

  • Write a JAVA code snippet which prints out numbers between 0 and 1,000,000, BUT only print...

    Write a JAVA code snippet which prints out numbers between 0 and 1,000,000, BUT only print the numbers which are a multiple of 3.

  • Below is a snippet of code that relies on three methods: execA, execB, and execC. The...

    Below is a snippet of code that relies on three methods: execA, execB, and execC. The worst, best, and average time complexity is given for each of these functions. int exec = random.nextInt(6); switch(exec) { case 0: execA(n); // O(n4), Omega(log n), Theta(n) break; case 5: execB(n); // O(n4), Omega(n), Theta(n) break; default: execC(n); // O(n3), Omega(n2), Theta(n*log n) } Please answer the following: 1. [2 marks] What is the worst case Big-O for this code? Justify your answer. 2....

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