opti trier_par_montant

This commit is contained in:
2026-10-21 11:17:36 +02:00
parent 2bd16d1b2c
commit 8b28803301
2 changed files with 23 additions and 10 deletions
+11 -10
View File
@@ -71,18 +71,19 @@ Transaction *generer_transactions(int n)
/* ------------------------------------------------------------------ */
/* F1 : trie les transactions par montant croissant, puis par id */
static int comparer_transactions(const void *a, const void *b)
{
const Transaction *x = a;
const Transaction *y = b;
if (x->montant != y->montant)
return (x->montant > y->montant) - (x->montant < y->montant);
return (x->id > y->id) - (x->id < y->id);
}
void trier_par_montant(Transaction *t, int n)
{
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (t[j].montant > t[j + 1].montant ||
(t[j].montant == t[j + 1].montant && t[j].id > t[j + 1].id)) {
Transaction tmp = t[j];
t[j] = t[j + 1];
t[j + 1] = tmp;
}
}
}
qsort(t, (size_t)n, sizeof(*t), comparer_transactions);
}
/* F2 : nombre de clients ayant au moins une transaction */