forked from menault/2026_TD1_DEV51_Qualite_Algo
Optimisé chercher_mot par recherche dichotomique
This commit is contained in:
+21
-6
@@ -235,13 +235,24 @@ int est_mot_vide(const char *mot)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Le tableau reste trié alphabétiquement pendant le comptage.
|
||||
* Renvoie l'indice du mot, ou -(position d'insertion + 1) s'il est absent. */
|
||||
int chercher_mot(Entree *table, int nb_entrees, const char *mot)
|
||||
{
|
||||
for (int i = 0; i < nb_entrees; i++) {
|
||||
if (egales(table[i].mot, mot))
|
||||
return i;
|
||||
int debut = 0;
|
||||
int fin = nb_entrees - 1;
|
||||
|
||||
while (debut <= fin) {
|
||||
int milieu = debut + (fin - debut) / 2;
|
||||
int comparaison = comparer(table[milieu].mot, mot);
|
||||
if (comparaison == 0)
|
||||
return milieu;
|
||||
if (comparaison < 0)
|
||||
debut = milieu + 1;
|
||||
else
|
||||
fin = milieu - 1;
|
||||
}
|
||||
return -1;
|
||||
return -(debut + 1);
|
||||
}
|
||||
|
||||
void compter_mot(Entree *table, int *nb_entrees, const char *mot)
|
||||
@@ -250,8 +261,12 @@ void compter_mot(Entree *table, int *nb_entrees, const char *mot)
|
||||
if (indice >= 0) {
|
||||
table[indice].occurrences++;
|
||||
} else {
|
||||
copier(table[*nb_entrees].mot, mot);
|
||||
table[*nb_entrees].occurrences = 1;
|
||||
indice = -indice - 1;
|
||||
/* Décaler uniquement lors de la première apparition d'un mot. */
|
||||
for (int i = *nb_entrees; i > indice; i--)
|
||||
table[i] = table[i - 1];
|
||||
copier(table[indice].mot, mot);
|
||||
table[indice].occurrences = 1;
|
||||
(*nb_entrees)++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user