forked from menault/2026_TD1_DEV51_Qualite_Algo
Remplacement du tri à bulles par un tri par tas pour améliorer les performances de tri
This commit is contained in:
+25
-8
@@ -319,16 +319,33 @@ int doit_echanger(const Entree *a, const Entree *b)
|
||||
return comparer(a->mot, b->mot) > 0;
|
||||
}
|
||||
|
||||
/* La racine du tas est l'entrée qui doit apparaître le plus tard. */
|
||||
void retablir_tas(Entree *table, int n, int racine)
|
||||
{
|
||||
while (racine < n / 2) {
|
||||
int enfant = 2 * racine + 1;
|
||||
if (enfant + 1 < n && doit_echanger(&table[enfant + 1], &table[enfant]))
|
||||
enfant++;
|
||||
if (!doit_echanger(&table[enfant], &table[racine]))
|
||||
break;
|
||||
|
||||
Entree tmp = table[racine];
|
||||
table[racine] = table[enfant];
|
||||
table[enfant] = tmp;
|
||||
racine = enfant;
|
||||
}
|
||||
}
|
||||
|
||||
void trier_entrees(Entree *table, int n)
|
||||
{
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - 1 - i; j++) {
|
||||
if (doit_echanger(&table[j], &table[j + 1])) {
|
||||
Entree tmp = table[j];
|
||||
table[j] = table[j + 1];
|
||||
table[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
/* Construire le tas, puis placer chaque racine à sa position finale. */
|
||||
for (int i = n / 2 - 1; i >= 0; i--)
|
||||
retablir_tas(table, n, i);
|
||||
for (int fin = n - 1; fin > 0; fin--) {
|
||||
Entree tmp = table[0];
|
||||
table[0] = table[fin];
|
||||
table[fin] = tmp;
|
||||
retablir_tas(table, fin, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user