Commit b0655a15 authored by Moïse Destin's avatar Moïse Destin
Browse files

change tas min du plus grand au plus peutit -> du plus petit au plus grand

parent c49fbbe7
Pipeline #4488 passed with stage
in 10 seconds
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="298fa7eb-9511-4983-9e33-1e1f5e3f9ebe" name="Changes" comment="" />
<list default="true" id="298fa7eb-9511-4983-9e33-1e1f5e3f9ebe" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Java/Tas.java" beforeDir="false" afterPath="$PROJECT_DIR$/Java/Tas.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......@@ -66,7 +69,7 @@
<workItem from="1635251919885" duration="31000" />
<workItem from="1635251962630" duration="689000" />
<workItem from="1635365695090" duration="598000" />
<workItem from="1635770461992" duration="21234000" />
<workItem from="1635770461992" duration="24949000" />
</task>
<servers />
</component>
......
public class Tas {
private static final int d= 2;
private int[] monTas;
private int capaciteTas; // controle reaching capacity d'insertion
private int positionDansTas; // controle reaching capacity d'insertion
private int nombreEchange = 0;
......@@ -9,7 +9,7 @@ public class Tas {
* initialiser tas
*/
public Tas(int taille){
capaciteTas = 0;
positionDansTas = 0;
monTas = new int[ taille+1]; //+0
}
......@@ -17,14 +17,20 @@ public class Tas {
* le tas est-il plein
*/
public boolean isTasPlein(){
return capaciteTas == monTas.length;
return positionDansTas == monTas.length;
}
private int parent(int pos) { return pos / 2; }
private int parent(int i){
return (i-1)/d;
}
private void echanger(int tas_courant_position, int tas_parent_position)
{
int tmp = monTas[tas_courant_position];
monTas[tas_courant_position] = monTas[tas_parent_position];
monTas[tas_parent_position] = tmp;
this.nombreEchange++;
}
/**
* ajoute un nouverau element dans le tas
......@@ -34,24 +40,19 @@ public class Tas {
if(isTasPlein())
throw new UnsupportedOperationException("le tas est plein, il n'y a plus de place pour inserer de nouveau element");
monTas[capaciteTas++] = x; // ajoute au dernier space vide
entasser(capaciteTas -1);
}
/**
* garder la propriete
*
*/
private void entasser(int i) {
int temp = monTas[i];
while(i>0 && temp > monTas[parent(i)]){
monTas[i] = monTas[parent(i)];
i = parent(i);
this.nombreEchange++;
int position_courant = positionDansTas;
monTas[position_courant] = x;
positionDansTas++;
while (monTas[position_courant] < monTas[parent(position_courant)]) {
echanger(position_courant, parent(position_courant));
position_courant = parent(position_courant);
}
monTas[i] = temp;
}
}
/**
* afficher tas
......@@ -60,7 +61,7 @@ public class Tas {
public void afficherTas()
{
System.out.print("Tas = ");
for (int i = 0; i < capaciteTas; i++)
for (int i = 0; i < positionDansTas; i++)
System.out.print(monTas[i] +" ");
System.out.println();
}
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment