Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
poo2019numeric
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
Model registry
Operate
Environments
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
francois.hofer
poo2019numeric
Commits
c0d9db78
Commit
c0d9db78
authored
5 years ago
by
loick.pipolo
Browse files
Options
Downloads
Patches
Plain Diff
binaryHeap compile
parent
185af25c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/ch/hepia/structure/BinaryHeap.java
+58
-38
58 additions, 38 deletions
src/main/java/ch/hepia/structure/BinaryHeap.java
with
58 additions
and
38 deletions
src/main/java/ch/hepia/structure/BinaryHeap.java
+
58
−
38
View file @
c0d9db78
java
.
lang
.
Math
java
.
util
.
Random
import
java.lang.Math
;
import
java.util.Random
;
public
class
BinaryHeap
{
//private int[] heap;
private
int
[]
heap
;
private
int
maxCap
;
private
int
nbOfElem
;
public
BinaryHeap
(
int
size
){
//this.heap = new int[10];
heap
=
int
[
size
];
maxCap
=
size
;
heap
=
new
int
[
size
];
nbOfElem
=
0
;
}
/*public BinaryHeap(int[] values){
int[] newHeap = new int[values.length];
for (int value : values){
for (int i = 0; i < newHeap.length; i++){
}
System.out.println(newHeap[1]);
}
this.heap = newHeap;
}*/
public
int
parent
(
int
i
){
return
(
i
-
1
)/
2
;
}
public
static
populate
(
int
nbValues
){
System
.
out
.
println
(
"gfgdg"
);
public
void
push
(
int
val
){
if
(
nbOfElem
==
maxCap
)
{
System
.
out
.
println
(
"Plus de place !"
);
return
;
}
// First insert the new key at the end
nbOfElem
++;
int
i
=
nbOfElem
-
1
;
heap
[
i
]
=
val
;
// Fix the min heap property if it is violated
while
(
i
!=
0
&&
heap
[
parent
(
i
)]
>
heap
[
i
])
{
int
temp
=
heap
[
i
];
heap
[
i
]
=
heap
[
parent
(
i
)];
heap
[
parent
(
i
)]
=
temp
;
i
=
parent
(
i
);
}
}
public
void
populate
(
int
nbValues
){
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
nbValues
;
i
++)
{
randomVal
=
Math
.
random
(
);
int
randomVal
=
random
.
nextInt
(
100
);
//System.out.println(randomVal);
this
.
heap
[
i
]
=
randomVal
;
this
.
push
(
randomVal
)
;
}
//return heap;
}
private
static
void
Echange
(
ref
int
a
,
ref
int
b
){
int
swap
=
a
;
a
=
b
;
b
=
swap
;
}
private
static
void
Tamiser
(
int
[]
arbre
,
int
noeud
,
int
n
){
/*private static void Tamiser(int[] arbre, int noeud, int n){
int k = noeud;
int j = 2 * k;
...
...
@@ -48,7 +58,9 @@ public class BinaryHeap{
if (arbre[k] < arbre[j])
{
Tri
.
Echange
(
ref
arbre
[
k
],
ref
arbre
[
j
]);
int swap = arbre[k];
arbre[k] = arbre[j];
arbre[j] = swap;
k = j;
j = 2 * k;
}
...
...
@@ -57,22 +69,30 @@ public class BinaryHeap{
}
}
public
static
void
TriParTas
(
BinaryHeap
arbre
){
for
(
int
i
=
arbre
.
L
ength
>>
1
;
i
>=
0
;
i
--)
Tri
.
Tamiser
(
arbre
,
i
,
arbre
.
L
ength
-
1
);
public
void heapSort(
){
for (int i =
this.heap.l
ength >> 1; i >= 0; i--)
Tamiser(
this.heap, i, this.heap.l
ength - 1);
for
(
int
i
=
arbre
.
L
ength
-
1
;
i
>=
1
;
i
--)
for (int i =
this.heap.l
ength - 1; i >= 1; i--)
{
Tri
.
Echange
(
ref
arbre
[
i
],
ref
arbre
[
0
]);
Tri
.
Tamiser
(
arbre
,
0
,
i
-
1
);
int swap = this.heap[i];
this.heap[i] = this.heap[0];
this.heap[0] = swap;
Tamiser(this.heap, 0, i - 1);
}
}*/
public
void
printHeap
(){
for
(
int
x
:
this
.
heap
)
{
System
.
out
.
println
(
x
);
}
}
public
static
void
main
(
String
[]
args
){
BinaryHeap
test
=
new
BinaryHeap
();
test
.
populate
(
5
);
//System.out.println(Arrays.toString(test));
System
.
out
.
println
(
"aaa"
);
BinaryHeap
test
=
new
BinaryHeap
(
10
);
test
.
populate
(
19
);
test
.
printHeap
(
);
}
}
\ No newline at end of file
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