forked from menault/TD4_DEV51_Qualite_Algo
il
This commit is contained in:
71
Bail.java
Normal file
71
Bail.java
Normal file
@@ -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<Object>() {
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
80
Ex3.java
Normal file
80
Ex3.java
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Ex3{
|
||||||
|
private int somme(int[]tab){
|
||||||
|
int somme =0
|
||||||
|
for(int i =0 ; i<tab.length();i++){
|
||||||
|
somme =+ tab[i];
|
||||||
|
}
|
||||||
|
return somme
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void bubblesort(int[] array,int profondeur){
|
||||||
|
if (profondeur ==1){
|
||||||
|
|
||||||
|
}
|
||||||
|
boolean swapped;
|
||||||
|
int valeur;
|
||||||
|
int length = array.length;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
swapped = false;
|
||||||
|
for(int i=1;i<length;i++)
|
||||||
|
{
|
||||||
|
if(array[i-1] > array[i])
|
||||||
|
{
|
||||||
|
valeur = array[i-1];
|
||||||
|
array[i-1] = array[i];
|
||||||
|
array[i] = valeur;
|
||||||
|
swapped=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while(swapped);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void test(List<Integer> tab,int profondeur){
|
||||||
|
for (int i=0;i<tab.length;i++){
|
||||||
|
bubblesort(tab[i]);
|
||||||
|
}
|
||||||
|
boolean swapped;
|
||||||
|
int[] tmp;
|
||||||
|
do {
|
||||||
|
swapped = false;
|
||||||
|
for (int i = 1; i < tab.length; i++) {
|
||||||
|
if (somme(tab[i - 1]) > 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
Exo2.md
Normal file
15
Exo2.md
Normal file
@@ -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
|
Reference in New Issue
Block a user