forked from abraham/2026_TD3_DEV51_Qualite_Algo
Compare commits
8 Commits
a5e748e993
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| cbc0cf780e | |||
| 64e3432b33 | |||
| 82a77ec17c | |||
| bf16afd727 | |||
| 96abb3e2d0 | |||
| 97155f2688 | |||
| 1e3ac9d8d2 | |||
| df4e8876fb |
+24
-2
@@ -6,7 +6,9 @@
|
||||
|
||||
# 1.Mesurer
|
||||
|
||||
```gcc -O2 banque.c -o banque && time ./banque```
|
||||
```
|
||||
gcc -O2 banque.c -o banque && time ./banque
|
||||
```
|
||||
|
||||
```
|
||||
Empreinte : ff68d260a7a58441
|
||||
@@ -16,7 +18,7 @@ user 0m4,772s
|
||||
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
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -103,14 +103,14 @@ int nb_clients_distincts(const Transaction *t, int n)
|
||||
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)
|
||||
{
|
||||
for (int c = 0; c < NB_CLIENTS; c++) {
|
||||
totaux[c] = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (t[i].client == c)
|
||||
totaux[c] += t[i].montant;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
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 meilleur = totaux[0];
|
||||
*debut = 0;
|
||||
*fin = 0;
|
||||
for (int i = 0; i < nb_jours; i++) {
|
||||
for (int j = i; j < nb_jours; j++) {
|
||||
long somme = 0;
|
||||
for (int k = i; k <= j; k++)
|
||||
somme += totaux[k];
|
||||
if (somme > meilleur) {
|
||||
meilleur = somme;
|
||||
*debut = i;
|
||||
*fin = j;
|
||||
}
|
||||
|
||||
long somme_courante = totaux[0];
|
||||
int debut_courant = 0;
|
||||
|
||||
for (int j = 1; j < nb_jours; j++) {
|
||||
if (somme_courante < 0) {
|
||||
somme_courante = totaux[j];
|
||||
debut_courant = j;
|
||||
} else {
|
||||
somme_courante += totaux[j];
|
||||
}
|
||||
if (somme_courante > meilleur) {
|
||||
meilleur = somme_courante;
|
||||
*debut = debut_courant;
|
||||
*fin = j;
|
||||
}
|
||||
}
|
||||
|
||||
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) */
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
/* 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;
|
||||
for (int i = 0; i < n; i++)
|
||||
for (int j = i + 1; j < n; j++)
|
||||
if (t[i].montant + t[j].montant == 0)
|
||||
int g = 0, d = n - 1;
|
||||
|
||||
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++;
|
||||
}
|
||||
g++; /* Puis on passe simplement au m[g] suivant ! */
|
||||
}
|
||||
}
|
||||
|
||||
free(m);
|
||||
return nb;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user