Compare commits

...

8 Commits

Author SHA1 Message Date
Karl Framery cbc0cf780e EXO 4 - optimisation F7 2026-10-21 12:20:55 +02:00
Karl Framery 64e3432b33 EXO 4 - optimisation F5 2026-10-21 11:58:37 +02:00
Karl Framery 82a77ec17c EXO 4 - optimisation F3 2026-10-21 11:32:06 +02:00
Karl Framery bf16afd727 EXO 4 - optimisation F3 2026-10-21 11:30:59 +02:00
Karl Framery 96abb3e2d0 EXO 4 - optimisation F3 2026-10-21 11:30:16 +02:00
Ozvann Abraham 97155f2688 Merge branch 'main' into main 2026-10-21 11:24:55 +02:00
abraham 1e3ac9d8d2 fonction 4 : O(n + J) 2026-10-21 11:19:08 +02:00
abraham df4e8876fb fonction 2 : O(n + C) 2026-10-21 11:10:03 +02:00
3 changed files with 80 additions and 20 deletions
+24 -2
View File
@@ -6,7 +6,9 @@
# 1.Mesurer # 1.Mesurer
```gcc -O2 banque.c -o banque && time ./banque``` ```
gcc -O2 banque.c -o banque && time ./banque
```
``` ```
Empreinte : ff68d260a7a58441 Empreinte : ff68d260a7a58441
@@ -16,7 +18,7 @@ user 0m4,772s
sys 0m0,005s sys 0m0,005s
``` ```
Le temps d'éxecution du programme est de 4.793 seconde et l'emprunte est <ff68d260a7a58441> Le temps d'éxecution du programme est de 4.793 seconde et l'emprunte est : [ff68d260a7a58441]
# 2.Profiler # 2.Profiler
@@ -80,7 +82,27 @@ La complexité de la fonction est de <span style="color:blue">[O(n)]</span> pour
# 4. Optimisation
- Optimisation de la fonction 1 nom du commit : ""
- Optimisation de la fonction 2 nom du commit : "fonction 2 : O(n + C)". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 3 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 4 nom du commit : "fonction 4 : O(n + J)". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 5 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 6 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 7 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 8 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 9 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 10 nom du commit : "". Emprunte : ff68d260a7a58441
BIN
View File
Binary file not shown.
+56 -18
View File
@@ -103,14 +103,14 @@ int nb_clients_distincts(const Transaction *t, int n)
return nb; return nb;
} }
/* F3 : solde total de chaque client */ /* F3 : solde total de chaque client en O(n + c) */
void total_par_client(const Transaction *t, int n, long *totaux) void total_par_client(const Transaction *t, int n, long *totaux)
{ {
for (int c = 0; c < NB_CLIENTS; c++) { for (int c = 0; c < NB_CLIENTS; c++) {
totaux[c] = 0; totaux[c] = 0;
for (int i = 0; i < n; i++) }
if (t[i].client == c) for (int i = 0; i < n; i++) {
totaux[c] += t[i].montant; totaux[t[i].client] += t[i].montant;
} }
} }
@@ -125,24 +125,30 @@ void total_par_jour(const Transaction *t, int n, long *totaux)
} }
} }
/* F5 : période de jours consécutifs dont la somme des soldes est maximale */ /* F5 : période de jours consécutifs dont la somme des soldes est maximale en O(J) */
long meilleure_periode(const long *totaux, int nb_jours, int *debut, int *fin) long meilleure_periode(const long *totaux, int nb_jours, int *debut, int *fin)
{ {
long meilleur = totaux[0]; long meilleur = totaux[0];
*debut = 0; *debut = 0;
*fin = 0; *fin = 0;
for (int i = 0; i < nb_jours; i++) {
for (int j = i; j < nb_jours; j++) { long somme_courante = totaux[0];
long somme = 0; int debut_courant = 0;
for (int k = i; k <= j; k++)
somme += totaux[k]; for (int j = 1; j < nb_jours; j++) {
if (somme > meilleur) { if (somme_courante < 0) {
meilleur = somme; somme_courante = totaux[j];
*debut = i; debut_courant = j;
*fin = j; } else {
} somme_courante += totaux[j];
}
if (somme_courante > meilleur) {
meilleur = somme_courante;
*debut = debut_courant;
*fin = j;
} }
} }
return meilleur; return meilleur;
} }
@@ -156,13 +162,45 @@ const Transaction *rechercher_par_id(const Transaction *t, int n, int id)
} }
/* F7 : nombre de paires de transactions de montants opposés (a + b = 0) */ /* F7 : nombre de paires de transactions de montants opposés (a + b = 0) */
static int cmp_long(const void *a, const void *b)
{
long x = *(const long *)a;
long y = *(const long *)b;
return (x > y) - (x < y);
}
long nb_paires_opposees(const Transaction *t, int n) long nb_paires_opposees(const Transaction *t, int n)
{ {
/* 1. Copier et trier les montants : O(n log n) */
long *m = malloc(n * sizeof(long));
for (int i = 0; i < n; i++) {
m[i] = t[i].montant;
}
qsort(m, n, sizeof(long), cmp_long);
/* 2. Deux pointeurs : O(n) au lieu de O(n²) */
long nb = 0; long nb = 0;
for (int i = 0; i < n; i++) int g = 0, d = n - 1;
for (int j = i + 1; j < n; j++)
if (t[i].montant + t[j].montant == 0) while (g < d) {
long somme = m[g] + m[d];
if (somme < 0) {
g++;
} else if (somme > 0) {
d--;
} else {
/* somme == 0 : on compte tous les m[d] identiques en face de m[g] */
for (int k = d; k > g && m[k] == m[d]; k--) {
nb++; nb++;
}
g++; /* Puis on passe simplement au m[g] suivant ! */
}
}
free(m);
return nb; return nb;
} }