Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
LectureCode INF102 V24
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders.Tokje
LectureCode INF102 V24
Commits
43707fbf
Commit
43707fbf
authored
4 months ago
by
Martin Vatshelle
Browse files
Options
Downloads
Patches
Plain Diff
Runtime analysis done in lecture
parent
1ab07640
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/h21/Products.java
+40
-0
40 additions, 0 deletions
src/main/java/h21/Products.java
src/main/java/h21/RuntimeAnalysis.java
+62
-0
62 additions, 0 deletions
src/main/java/h21/RuntimeAnalysis.java
with
102 additions
and
0 deletions
src/main/java/h21/Products.java
0 → 100644
+
40
−
0
View file @
43707fbf
package
h21
;
import
java.util.Arrays
;
import
java.util.HashMap
;
public
class
Products
{
public
static
int
countProducts
(
int
[]
a
)
{
int
total
=
0
;
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
for
(
int
j
=
i
;
j
<
a
.
length
;
j
++)
{
int
prod
=
a
[
i
]*
a
[
j
];
for
(
int
k
=
0
;
k
<
a
.
length
;
k
++)
{
if
(
a
[
k
]==
prod
)
total
++;
}
}
}
return
total
;
}
//TODO: improve this
public
static
int
countProducts2
(
int
[]
a
)
{
int
total
=
0
;
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
{
for
(
int
j
=
i
;
j
<
a
.
length
;
j
++)
{
int
prod
=
a
[
i
]*
a
[
j
];
for
(
int
k
=
0
;
k
<
a
.
length
;
k
++)
{
if
(
a
[
k
]==
prod
)
total
++;
}
}
}
return
total
;
}
}
This diff is collapsed.
Click to expand it.
src/main/java/h21/RuntimeAnalysis.java
0 → 100644
+
62
−
0
View file @
43707fbf
package
h21
;
public
class
RuntimeAnalysis
{
public
static
int
numPairs
(
int
n
)
{
int
count
=
0
;
//1*O(1)
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
//n iterasjoner
for
(
int
j
=
i
+
1
;
j
<
n
;
j
++)
{
//O(n) iterasjoner
count
++;
//O(n^2) * O(1)
}
}
return
count
;
}
public
static
void
printProducts
(
int
n
)
{
int
a
=
0
;
//O(1)
int
b
=
n
;
//O(1)
while
(
a
<=
b
)
{
//O(n) iterasjoner
if
(
a
*
b
==
n
)
{
System
.
out
.
println
(
a
+
"*"
+
b
+
"="
+
a
*
b
);
//O(1)
a
++;
//O(1)
b
--;
//O(1)
}
else
if
(
a
*
b
>
n
)
b
--;
//O(1)
else
if
(
a
*
b
<
n
)
a
++;
//O(1)
}
}
public
static
void
printProducts2
(
int
n
)
{
int
a
=
1
;
//O(1)
int
b
=
n
;
//O(1)
while
(
a
<=
b
)
{
//O(sqrt(n))
if
(
a
*
b
==
n
)
{
System
.
out
.
println
(
a
+
"*"
+
b
+
"="
+
a
*
b
);
//O(1)
a
++;
//O(1)
b
--;
//O(1)
}
else
{
if
(
a
*
b
>
n
)
b
=
n
/
a
;
//O(1)
else
if
(
a
*
b
<
n
)
a
++;
//O(1)
}
}
}
public
static
String
loopDeLoop
(
int
n
)
{
String
lyrics
=
""
;
//O(1)
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
//n iterasjoner
lyrics
+=
"Loop de loop, flip flop.\n"
;
//O(lyrics.length)= O(n)
lyrics
+=
"Flying in an aeroplane.\n"
;
}
return
lyrics
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment