FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structures-Java/ds/BigO.java at master · davidjurado/Data-Structures-Java · GitHub
davidjurado
/
Data-Structures-Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structures-Java
/
ds
/
BigO.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (59 loc) · 1.75 KB
Breadcrumbs
Data-Structures-Java
/
ds
/
BigO.java
Copy path
File metadata and controls
82 lines (59 loc) · 1.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
ds
;
public
class
BigO
{
/*
*1. Operaciones aritméticas (+, -, *, /, ^, %), y comparaciones toman solo 1 paso para ejecutarse.
*2. Ciclos y subrutinas toman n pasos para ejecutarse.
*3. Cualquier acceso a memoria toma un paso en ejecutarse.
*/
public
static
void
main
(
String
[]
args
) {
doublingLoopVariable
(
500
);
}
static
void
simpleFunction
(
int
n
){
int
a
=
9
;
//O(1)
int
b
=
3
;
//O(1)
int
sum
=
a
+
b
+
n
;
//O(1)
int
product
=
a
*
b
*
n
;
//O(1)
int
quotient
=
a
*
n
/
b
;
//O(1)
System
.
out
.
println
(
String
.
format
(
"The summ is: %s, product is: %s and quotient is: %s"
,
sum
,
product
,
quotient
));
//O(1)
//O(1)+O(1)+O(1)+O(1)+O(1)+O(1)=O(1)
}
public
static
void
singleForLoop
(
int
n
){
for
(
int
i
=
0
;
i
<
n
;
i
++){
//O(n)
System
.
out
.
println
(
String
.
format
(
"Square of %s is: %s"
,
i
,
Math
.
pow
(
i
,
2.0
)));
//O(1)
}
//O(n)*O(1)=O(n)
}
public
static
void
nestedForLoop
(
int
n
){
for
(
int
i
=
0
;
i
<
n
;
i
++){
//O(n)
for
(
int
j
=
0
;
j
<
n
;
j
++){
//O(n)
System
.
out
.
println
(
String
.
format
(
"Product of %s and %s is: %s"
,
i
,
j
,
i
*
j
));
//O(1)
}
}
//O(n)*O(n)*O(1)=O(n^2)
}
public
static
void
twoForLoops
(
int
n
){
for
(
int
i
=
0
;
i
<
n
;
i
++){
//O(n)
System
.
out
.
println
(
"Printing: "
+
i
);
//O(1)
}
for
(
int
i
=
0
;
i
<
1000
;
i
++){
//O(1000)
System
.
out
.
println
(
"Printing: "
+
i
);
//O(1)
}
//O(n)*O(1)+O(1000)*O(1)=O(n)*O(1000)=O(n)
}
public
static
void
twoForLoopsNAndM
(
int
n
,
int
m
){
for
(
int
i
=
0
;
i
<
n
;
i
++){
//O(n)
for
(
int
j
=
0
;
j
<
m
;
j
++){
//O(m)
System
.
out
.
println
(
"Printing: "
+ (
i
*
j
));
//O(1)
}
}
//O(n)*O(m)*O(1)=O(n*m)
}
public
static
void
doublingLoopVariable
(
int
n
){
for
(
int
i
=
1
;
i
<
n
;){
//O(Log(n))
System
.
out
.
println
(
"Value of i is: "
+
i
);
//O(1)
i
=
i
*
2
;
//O(1)
}
//O(Log(n))*(O(1)+O(1))=O(Log(n))
}
}
Back
|
FazBrowse Home
|
New Git URL