-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigO_Mystery
47 lines (38 loc) · 1.53 KB
/
BigO_Mystery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bigo_mystery;
public class BigO_Mystery {
public static void main(String[] args) {
int s1_steps = 0, s2_steps= 0, s3_steps = 0, s4_steps =0, s5_steps = 0;
int s6_steps = 0;
int N;
System.out.print("Enter an integer value for N: ");
N = new java.util.Scanner(System.in).nextInt(); //look-anonymous object
int s = 0; s1_steps++; //s1
s2_steps++;
for (int i=1; i<=N; s2_steps++, i++) //s2
{
s3_steps++;
for (int j = 1; j<=N; s3_steps++, j++) //s3
{
s4_steps++; //s4
s = s+j;
}// end inner j loop
s5_steps++;
for (int k=1; k<=N;k++) //s5
{
s = s+k;
s6_steps++; //s6
}// end innermost k for loop
}// end i for loop
System.out.printf("S1 executed %d times %n", s1_steps);
System.out.printf("S2 executed %d times %n", s2_steps);
System.out.printf("S3 executed %d times %n", s3_steps);
System.out.printf("S4 executed %d times %n", s4_steps);
System.out.printf("S5 executed %d times %n", s5_steps);
System.out.printf("S6 executed %d times %n", s6_steps);
}
}