diff --git a/Bail.java b/Bail.java new file mode 100644 index 0000000..33dca70 --- /dev/null +++ b/Bail.java @@ -0,0 +1,71 @@ +import java.util.Arrays; +import java.util.Comparator; + +public class Bail { + + public static void main(String[] args) { + // Exemple de tableau à 3 dimensions + Integer[][][] tableau3D = { + { {3, 1, 2}, {9, 0, 8} }, + { {4, 5, 1}, {2, 2, 2} } + }; + + // Appel de la fonction de tri + trierND(tableau3D); + + // Affichage du tableau trié + System.out.println("Tableau trié : "); + System.out.println(Arrays.deepToString(tableau3D)); + } + + /** + * Fonction qui trie un tableau de n'importe quelle dimension. + * Si c'est un tableau d'entiers, on le trie directement. + * Sinon, on trie les sous-tableaux récursivement, puis le tableau actuel + * selon la somme des sous-tableaux. + */ + public static void trierND(Object tableau) { + if (tableau instanceof int[]) { + // Si on est au dernier niveau (tableau d'entiers) + Arrays.sort((int[]) tableau); + } else if (tableau instanceof Object[]) { + Object[] tab = (Object[]) tableau; + + // On trie d'abord les sous-tableaux (appel récursif) + for (Object sousTab : tab) { + trierND(sousTab); + } + + // Puis on trie le tableau courant selon la somme de ses sous-tableaux + Arrays.sort(tab, new Comparator() { + @Override + public int compare(Object a, Object b) { + int sommeA = sommeRecursive(a); + int sommeB = sommeRecursive(b); + return Integer.compare(sommeA, sommeB); + } + }); + } + } + + /** + * Fonction qui calcule récursivement la somme d'un tableau à N dimensions. + */ + public static int sommeRecursive(Object tableau) { + if (tableau instanceof int[]) { + int somme = 0; + for (int valeur : (int[]) tableau) { + somme += valeur; + } + return somme; + } else if (tableau instanceof Object[]) { + int sommeTotale = 0; + for (Object sousTab : (Object[]) tableau) { + sommeTotale += sommeRecursive(sousTab); + } + return sommeTotale; + } else { + return 0; + } + } +} diff --git a/Ex3.class b/Ex3.class new file mode 100644 index 0000000..b123b3c Binary files /dev/null and b/Ex3.class differ diff --git a/Ex3.java b/Ex3.java new file mode 100644 index 0000000..15732c1 --- /dev/null +++ b/Ex3.java @@ -0,0 +1,80 @@ + +import java.util.Arrays; + +public class Ex3{ + private int somme(int[]tab){ + int somme =0 + for(int i =0 ; i array[i]) + { + valeur = array[i-1]; + array[i-1] = array[i]; + array[i] = valeur; + swapped=true; + } + } + } while(swapped); + } + + + + public static void test(List tab,int profondeur){ + for (int i=0;i somme(tab[i])) { + tmp = tab[i - 1]; + tab[i - 1] = tab[i]; + tab[i] = tmp; + swapped = true; + } + } + } while (swapped); + } + + + + public static void main(String[] args) { + int[][] tableau = { + {0, 3, 2}, + {9, 4, 5}, + {4, 1, 3} + }; + + System.out.println("Avant tri :"); + for (int[] ligne : tableau) { + System.out.println(Arrays.toString(ligne)); + } + + // Appel du tri + test(tableau); + + System.out.println("\nAprès tri :"); + for (int[] ligne : tableau) { + System.out.println(Arrays.toString(ligne)); + } + } + +} \ No newline at end of file diff --git a/Exo2.md b/Exo2.md new file mode 100644 index 0000000..100dbc5 --- /dev/null +++ b/Exo2.md @@ -0,0 +1,15 @@ +Marvin Lebreton et Maxime AUbert + +# Ex2 + +## 1: + +La réponse est o(n*m) car y'a un tableau imbriqué dans un autre tableau + +## 2: + +La réponse est o(n) parce que le nombre d'itération de la boucle dépend de la val de n + +## 3: + +La réponse est o(1), peu importe la val de x, on va toujours toujours au max faire 3 instruction \ No newline at end of file