Bonjour, j'essaye d'écrire un tri fusion pour un tableau de String mais il semble il y avoir des erreurs que je n'arrive pas à trouver.
Pour autant ma fonction fusion à l'air de plutôt bien marcher mais dès que j'appelle la fonction tri_fusion certains mots de mon tableau se répètent et d'autres sont supprimé.
Si quelqu'un peux m'éclairer ce serait sympa !
Merci d'avance !
public static String[] fusion (String[] tab, int deb, int m, int f) {
String[] tab_g = new String[m - deb + 1]; // initialition des tableaux droite et gauche
String[] tab_d = new String[f - m + 1];
for (int i = 0; i < tab_g.length; i++) { // copie de la partie gauche du tableau
tab_g* = tab*;
}
tab_g[tab_g.length-1] = "STOP"; // création d'une sentinelle
int g = 0;
for (int i = m; i < f; i++) { // copie de la partie de droite du tableau
tab_d[g] = tab*;
g++;
}
tab_d[tab_d.length - 1] = "STOP"; // création d'une sentinelle
int i = 0;
int j = 0;
int s = 0;
for (int k = 0; k < tab.length &&(j != tab_d.length-1 || i != tab_g.length-1); k++) { // boucle qui réunit les deux tableaux
if (s == 0) {
if ((tab_g*.equalsIgnoreCase("STOP")) == true) {
s = 1;
}
if ((tab_d[j].equalsIgnoreCase("STOP")) == true) {
s = 2;
}
if (tab_g*.compareTo(tab_d[j]) <= 0 && s == 0) {
tab[k] = tab_g*;
i++;
} else if (s == 0) {
tab[k] = tab_d[j];
j++;
}
}
if (s == 1 && j != tab_d.length-1) {
tab[k] = tab_d[j];
j++;
} else if (s == 2 && i != tab_g.length-1) {
tab[k] = tab_g*;
i++;
}
}
return tab;
}
public static String[] tri_fusion (String[] tab, int deb, int f) {
if (f<=deb) {
return tab;
} else {
int milieu = deb+(f-deb)/2;
tri_fusion(tab,deb,milieu);
tri_fusion(tab, milieu+1,f);
return fusion(tab, deb, milieu,f);
}
}