Compare commits

2 Commits
main ... main

Author SHA1 Message Date
c15bde258f complexité 2025-10-15 11:45:46 +02:00
1c132742fa Ex2/Ex3 2025-10-15 11:41:50 +02:00
3 changed files with 80 additions and 0 deletions

14
Ex_2/Complexité.txt Normal file
View File

@@ -0,0 +1,14 @@
Ex 2 - Calculs de complexité de fonctions
function_1:
Dans la premier fonction il y une boucle qui parcours le premier tableau (tableau1) = n.
Ensuite il y une autre boucle qui parcour le deuxeime tableau (tableau2) = m.
Ce qui donne la complexité suivante : O(nxm).
function_2:
Comme il s'agit d'une simple boucle while la complexité de la function_2 est simplement O(n).
function_3:
Chacune des instructions ne se produise qu'une fois (pas de répétition, boucle) donc la complexité est de O(1).

BIN
Ex_3/Algorithme.class Normal file

Binary file not shown.

66
Ex_3/Algorithme.java Normal file
View File

@@ -0,0 +1,66 @@
import java.util.*;
import java.util.stream.Collectors;
public class Algorithme {
// O(n²m+nm²)
public static void Algo(int[][] a) {
if (a == null) throw new IllegalArgumentException("Tableau null");
int n = a.length;
if (n == 0) throw new IllegalArgumentException("Tableau vide");
int m = a[0].length;
for (int i = 0; i < n; i++) {
if (a[i] == null || a[i].length != m)
throw new IllegalArgumentException("Tous les sous-tableaux doivent avoir la même taille");
}
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (sum(a[j]) < sum(a[min])) min = j;
}
int[] tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
for (int i = 0; i < n; i++) selection(a[i]);
}
// O(n²)
public static void selection(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) min = j;
}
int tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
// O(n)
public static int sum(int[] arr) {
int s = 0;
for (int x : arr) s += x;
return s;
}
// O(n²m+nm²)
public static void main(String[] args) {
int[][] t = {
{0, 3, 2},
{9, 4, 5},
{4, 1, 3},
{77, 12, 56},
{6, 4, 9}
};
Algo(t);
for (int[] r : t) {
for (int x : r) System.out.print(x + " ");
System.out.println();
}
}
}