forked from menault/TD4_DEV51_Qualite_Algo
63 lines
1.7 KiB
Java
63 lines
1.7 KiB
Java
|
import java.util.Arrays;
|
||
|
|
||
|
public class TriDeTableau {
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
int[][] tableau = {
|
||
|
{ 0, 3, 2 }, { 9, 4, 5 }, { 4, 1, 3 }
|
||
|
};
|
||
|
|
||
|
int[][] tableautrier = TriTableau(tableau);
|
||
|
|
||
|
for (int[] listTableau : tableautrier) {
|
||
|
System.out.println(Arrays.toString(listTableau));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static int[][] TriTableau(int[][] tableau) {
|
||
|
// Tri individuel des sous-tableaux
|
||
|
for (int[] listTableau : tableau) {
|
||
|
TriDansTableau(listTableau);
|
||
|
}
|
||
|
|
||
|
// Tri des sous-tableaux en fonction de leur somme
|
||
|
boolean swap;
|
||
|
do {
|
||
|
swap = false;
|
||
|
for (int i = 1; i < tableau.length; i++) {
|
||
|
if (sommetableau(tableau[i - 1]) > sommetableau(tableau[i])) {
|
||
|
|
||
|
int[] temp = tableau[i - 1];
|
||
|
tableau[i - 1] = tableau[i];
|
||
|
tableau[i] = temp;
|
||
|
swap = true;
|
||
|
}
|
||
|
}
|
||
|
} while (swap);
|
||
|
return tableau;
|
||
|
}
|
||
|
|
||
|
public static void TriDansTableau(int[] tableau) {
|
||
|
boolean swap;
|
||
|
do {
|
||
|
swap = false;
|
||
|
for (int i = 1; i < tableau.length; i++) {
|
||
|
if (tableau[i - 1] > tableau[i]) {
|
||
|
|
||
|
int temp = tableau[i - 1];
|
||
|
tableau[i - 1] = tableau[i];
|
||
|
tableau[i] = temp;
|
||
|
swap = true;
|
||
|
}
|
||
|
}
|
||
|
} while (swap);
|
||
|
}
|
||
|
|
||
|
public static int sommetableau(int[] tableau) {
|
||
|
int somme = 0;
|
||
|
for (int chiffre : tableau) {
|
||
|
somme += chiffre;
|
||
|
}
|
||
|
return somme;
|
||
|
}
|
||
|
}
|