Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
2e43d611ee | |||
7a1e660855 |
BIN
Algo$1.class
Normal file
BIN
Algo$1.class
Normal file
Binary file not shown.
BIN
Algo.class
Normal file
BIN
Algo.class
Normal file
Binary file not shown.
70
Algo.java
Normal file
70
Algo.java
Normal file
@@ -0,0 +1,70 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Algo {
|
||||
|
||||
public static Object sortNDArray(Object array) {
|
||||
if (array instanceof int[]) {
|
||||
int[] arr = (int[]) array;
|
||||
Arrays.sort(arr);
|
||||
return arr;
|
||||
} else if (array instanceof Object[]) {
|
||||
Object[] arr = (Object[]) array;
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
arr[i] = sortNDArray(arr[i]);
|
||||
}
|
||||
|
||||
Arrays.sort(arr, new Comparator<Object>() {
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
int sum1 = getSum(o1);
|
||||
int sum2 = getSum(o2);
|
||||
return Integer.compare(sum1, sum2);
|
||||
}
|
||||
});
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int getSum(Object array) {
|
||||
if (array instanceof int[]) {
|
||||
int sum = 0;
|
||||
for (int val : (int[]) array) {
|
||||
sum += val;
|
||||
}
|
||||
return sum;
|
||||
} else if (array instanceof Object[]) {
|
||||
int sum = 0;
|
||||
for (Object sub : (Object[]) array) {
|
||||
sum += getSum(sub);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void printArray(Object array) {
|
||||
if (array instanceof int[]) {
|
||||
System.out.println(Arrays.toString((int[]) array));
|
||||
} else if (array instanceof Object[]) {
|
||||
for (Object sub : (Object[]) array) {
|
||||
printArray(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object[] array2D = new Object[] {
|
||||
new int[] {0, 3, 2},
|
||||
new int[] {9, 4, 5},
|
||||
new int[] {4, 1, 3},
|
||||
new int[] {4, 1, 3}
|
||||
};
|
||||
|
||||
Object sorted = sortNDArray(array2D);
|
||||
printArray(sorted);
|
||||
}
|
||||
}
|
BIN
fonction2.class
Normal file
BIN
fonction2.class
Normal file
Binary file not shown.
17
readme.md
Normal file
17
readme.md
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
# TD 4
|
||||
|
||||
## Ex 2
|
||||
|
||||
Fonction 1 : n1*n2
|
||||
Le résultat dépend de 2 valeurs différentes.
|
||||
|
||||
Fonction 2 : 0(n)
|
||||
La fonction tourne x fois car on a x qui représente un compteur et quand x vaut 0 la fonction se termine.
|
||||
|
||||
Fonction 3 : 0(1)
|
||||
La fonction de ne s'exécute que 4 instructions.
|
||||
|
||||
## Ex 3
|
||||
|
||||
O(M**n+1 * logM)
|
Reference in New Issue
Block a user