-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexity.java
More file actions
83 lines (73 loc) · 1.74 KB
/
Complexity.java
File metadata and controls
83 lines (73 loc) · 1.74 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package OrderOfComplexityCalculator;
/**
* Ritesh Sunil Chavan
*/
/**
* This class prints the order of complexities and stores the power for n and log(n)
*/
public class Complexity {
/**
* This variable stores the power of n
*/
private int n_power;
/**
* This variable stores the power value of log(n)
*/
private int log_power;
/**
* Getter methods for n_power
* @return power of n
*/
public int getN_power() {
return n_power;
}
/**
* Setter method for n_power
* @param n_power power of n
*/
public void setN_power(int n_power) {
this.n_power = n_power;
}
/**
* Getter method for log_power
* @return power of log(n)
*/
public int getLog_power() {
return log_power;
}
/**
* Setter method for log_power
* @param log_power power of log
*/
public void setLog_power(int log_power) {
this.log_power = log_power;
}
/**
* Prints order of complexity
* @return order of complexity
*/
public String toString() {
String result="O(1)";
String nresult="";
String logresult="";
if (n_power==1)
nresult="n";
else if (n_power>1) {
nresult="n^"+n_power;
}
if (log_power==1)
logresult="log(n)";
else if (log_power>1)
logresult="log(n)^"+log_power;
if (!nresult.isBlank()&&!logresult.isBlank())
{
result="O("+nresult+" * "+logresult+")";
} else if (!nresult.isBlank()) {
result="O("+nresult+")";
}
else if (!logresult.isBlank()) {
result="O("+logresult+")";
}
return result;
}
}