Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each of the two program fragments: a) Implement the code in java and give th

ID: 638358 • Letter: F

Question

For each of the two program fragments: a) Implement the code in java and give the running time for several values of N. (for example, 50, 100, 500, 1000 N values at minimum). Average the result of 50 runs for each test N.

Program Fragment 1

Sum = 0;

For (I = 0; I < n; I++)

     For (J = 0; J < I; J++)

           Sum++:

Program Fragment 2

Sum = 0;

For (I = 1; I < N; I++)

   For (J = 1; J < I * I; J++)

       If (J % I == 0)

           For (K = 0; K < J; K++)

               Sum++;

Java source code file(s).

Java source code must compile.

Sum = 0;

For (I = 0; I < n; I++)

     For (J = 0; J < I; J++)

           Sum++:

Explanation / Answer

1)

public class HelloWorld{

public static void main(String []args){
System.out.println("Hello World");
int Sum = 0,n=50;

for (int I = 0; I < n; I++)

for (int J = 0; J < I; J++)

Sum++;
System.out.println(Sum);   
}
}

complexity is O(n2)

2)

public class HelloWorld{

public static void main(String []args){
System.out.println("Hello World");
int Sum = 0,N=50;

for (int I = 1; I < N; I++)

for (int J = 1; J < I * I; J++)

if (J % I == 0)

for (int K = 0; K < J; K++)

Sum++;
System.out.println(Sum);   
}
}

O(n3)