Skip to content
Snippets Groups Projects
Commit c0d9db78 authored by loick.pipolo's avatar loick.pipolo
Browse files

binaryHeap compile

parent 185af25c
Branches
Tags
No related merge requests found
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.Length >> 1; i >= 0; i--)
Tri.Tamiser(arbre, i, arbre.Length - 1);
public void heapSort(){
for (int i = this.heap.length >> 1; i >= 0; i--)
Tamiser(this.heap, i, this.heap.length - 1);
for (int i = arbre.Length - 1; i >= 1; i--)
for (int i = this.heap.length - 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment