F1 : réduction de O(n²) à O(n log n)

This commit is contained in:
2026-10-21 12:21:45 +02:00
parent 25c08dc33f
commit 6d95a3f2a2
4 changed files with 11 additions and 10 deletions
BIN
View File
Binary file not shown.
+8 -10
View File
@@ -71,18 +71,16 @@ Transaction *generer_transactions(int n)
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* F1 : trie les transactions par montant croissant, puis par id */ /* F1 : trie les transactions par montant croissant, puis par id */
int compareFunction(const void *ptr1, const void *ptr2){
const Transaction *t1 = ptr1, *t2 = ptr2;
if(t1->montant != t2->montant)
return (t1->montant > t2->montant) - (t1->montant < t2->montant);
return (t1->id > t2->id) - (t1->id < t2->id);
}
void trier_par_montant(Transaction *t, int n) void trier_par_montant(Transaction *t, int n)
{ {
for (int i = 0; i < n - 1; i++) { qsort(t, n, sizeof(Transaction), compareFunction);
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;
}
}
}
} }
/* F2 : nombre de clients ayant au moins une transaction */ /* F2 : nombre de clients ayant au moins une transaction */
Regular → Executable
View File
+3
View File
@@ -0,0 +1,3 @@
zaabay;-;2230
zaabay;-;946
zaabay;-;913
1 zaabay - 2230
2 zaabay - 946
3 zaabay - 913