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...

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

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

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

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

  • For Question 14 - 15, please refer to the following code snippet. 1 class Fitness 2...

    For Question 14 - 15, please refer to the following code snippet. 1 class Fitness 2 { 3 int Jumping; int Running; 12 }; 8 class Round ISAShape: public class Fitness 9 { 10 11 int Sleeping; int Eating; 13 }; 14. The following statements are TRUE based on the code snippet above, EXCEPT (A) class RoundISAShape is inheriting from class Fitness (B) class RoundISAShape is the base class for class Fitness (C) class Round ISAShape is considered as the...

  • Question 6 (1 point) What is the output of the following code snippet? Please ensure your...

    Question 6 (1 point) What is the output of the following code snippet? Please ensure your answe. spaced according to the program code output. public static void main(String[] args) int channel = assignChannel (2); System.out.println("Channel: " + channel); ) public static int assignChannel(int channel) return channel + 3; > A/

  • Consider the blowing code snippet Question 4 2 pts 2 pts Canvas XC Question 3 2...

    Consider the blowing code snippet Question 4 2 pts 2 pts Canvas XC Question 3 2 pts Consider the following code snippet public class Hypp extends Application public void start (Stage stapelt Button button new lutton("Calculate"); Label label new Label("Total amount dut) > public class Handler rolesents EventHandler Action vest public void handle (Actiontrent event) label.Text("Hell) > 2 What is wrong with this code? label must be declared as an instance variable. button must be declared as an instance variable...

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