diff --git a/slides/cours_13.md b/slides/cours_13.md index c1edd14d447949e532506081d9355bdd0cf57cc2..0c6990f1138b6eabb630272a06d9bfbec5dcaf3e 100644 --- a/slides/cours_13.md +++ b/slides/cours_13.md @@ -71,6 +71,122 @@ On parle de paires *clé-valeur* (*key-value pairs*). * Consultation (`get(clé)`{.C}), retourne la `valeur` correspondant à `clé` * Suppression (`remove(clé)`{.C}), supprime la paire `clé-valeur` -## Structure de données +## Structure de données / implémentation + +Efficacité dépend de différents paramètres: + +* taille (nombre de clé-valeurs maximal), +* fréquence d'utilisation (insertion, consultation, suppression), +* données triées/non-triées, +* ... + +# Consultation séquentielle (`sequential_get`) + +## Séquentielle + +* table représentée par un (petit) tableau ou liste chaînée, +* types: `key_t` et `value_t` quelconques, et `key_value_t` + + ```C + typedef struct { + key_t key; + value_t value; + } key_value_t; + ``` +* on recherche l'existence de la clé séquentiellement dans le tableau, on + retourne la valeur. + +# Consultation séquentielle (`sequential_get`) + +## Implémentation? Une idée? + +. . . + +```C +bool sequential(int n, key_value_t table[n], key_t key, value_t *value) { + int pos = n - 1; + while (pos >= 0) { + if (key == table[pos].key) { + *value = table[pos].value; + return true; + } + pos--; + } + return false; +} +``` + +. . . + +## Inconvénient? + +# Consultation séquentielle (`sequential_get`) + +## Exercice: implémenter la même fonction avec une liste chaînée + +Poster le résultat sur matrix. + +# Consultation dichotomique (`binary_get`) + +## Dichotomique + +* table représentée par un (petit) tableau trié par les clés, +* types: `key_t` et `value_t` quelconques, et `key_value_t` +* on recherche l'existence de la clé par dichotomie dans le tableau, on + retourne la valeur, +* les clés possèdent la notion d'ordre (`<, >, =` sont définis). + +# Consultation dichotomique (`binary_get`) + +## Implémentation? Une idée? + +. . . + +```C +bool binary_get1(int n, value_key_t table[n], key_t key, value_t *value) { + int top = n - 1, bottom = 0; + while (top > bottom) { + int middle = (top + bottom) / 2; + if (x > table[middle]) { + bottom = middle+1; + } else { + top = middle; + } + } + if (key == table[top].key) { + *value = table[top].value; + return true; + } else { + return false; + } +} +``` + +# Consultation dichotomique (`binary_get`) + +## Autre implémentation + +```C +bool binary_get2(int n, key_value_t table[n], key_t key, value_t *value) { + int top = n - 1, bottom = 0; + while (true) { + int middle = (top + bottom) / 2; + if (key > table[middle].key) { + bottom = middle + 1; + } else if (key < table[middle].key) { + top = middle; + } else { + *value = table[middle].value; + return true; + } + if (top < bottom) { + break; + } + } + return false; +} +``` + +## Quelle est la différence avec le code précédent? diff --git a/source_codes/complexity/sum.c b/source_codes/complexity/sum.c index 4a9780aba94c6454def947222c12d2c5b88738a8..44e93033acb1430ec923d8a5b90871252b4475e6 100644 --- a/source_codes/complexity/sum.c +++ b/source_codes/complexity/sum.c @@ -7,7 +7,7 @@ #endif #ifndef NUM_TIMES -#define NUM_TIMES 10 +#define NUM_TIMES 100 #endif void init(int n, double tab[]) { diff --git a/source_codes/loops_tests/.gitignore b/source_codes/loops_tests/.gitignore index 0a63c70f19ad8d2da7572535b0867a0f8c951bac..244441b26b33cdf970beee358b5b6e2047bcc309 100644 --- a/source_codes/loops_tests/.gitignore +++ b/source_codes/loops_tests/.gitignore @@ -3,3 +3,8 @@ ppcm_bis nb1er pgcd ppcm +ppcm +ppcm_bis +factorielle +nb1er +pgcd diff --git a/source_codes/pointeurs/.gitignore b/source_codes/pointeurs/.gitignore index 7bf15f4ab3ca13512819187a45277f86dc8854ec..d59933696d126a7fec217180c7dfbb7e6b32c6ba 100644 --- a/source_codes/pointeurs/.gitignore +++ b/source_codes/pointeurs/.gitignore @@ -1 +1,2 @@ string_deep_shallow_copy +string_deep_shallow_copy diff --git a/source_codes/recursivity/rec_fibonacci.c b/source_codes/recursivity/rec_fibonacci.c index eb134a868a5855ec01efce19537b652ad96030ee..939520640ba4e490be80eb3fbffbeff73439303c 100644 --- a/source_codes/recursivity/rec_fibonacci.c +++ b/source_codes/recursivity/rec_fibonacci.c @@ -3,6 +3,7 @@ // Suite de Fibonacci: a_n = a_{n-1} + a_{n-2}, a_0 = 0, a_1 = 1 int fib(int n); +int fib2(int n); int fib_imp(int n); int main() { @@ -10,6 +11,7 @@ int main() { printf("n="); scanf("%d", &n); printf("%d\n", fib(n)); + printf("%d\n", fib2(n)); printf("%d\n", fib_imp(n)); } @@ -25,6 +27,13 @@ int fib(int n) { } } +int fib2(int n) { + if (n > 1) { + return fib2(n - 1) + fib(n - 2); + } + return n; +} + int fib_imp(int n) { int fib0 = 1; int fib1 = 1; diff --git a/source_codes/recursivity/rec_ppcm.c b/source_codes/recursivity/rec_ppcm.c index ef505ee8dc07c860ff36c6b5ed39cc0a1dbe6d2e..bc4ba9accef4d7788e5757fa1b094b918a803188 100644 --- a/source_codes/recursivity/rec_ppcm.c +++ b/source_codes/recursivity/rec_ppcm.c @@ -1,24 +1,24 @@ #include <stdio.h> #include <stdlib.h> -int ppcm(int mult_n,int mult_m,int n,int m); +int ppcm(int mult_n, int mult_m, int n, int m); void main() { - int n,m; - printf("n="); - scanf("%d",&n); - printf("m="); - scanf("%d",&m); - printf("%d\n",ppcm(n,m,n,m)); + int n, m; + printf("n="); + scanf("%d", &n); + printf("m="); + scanf("%d", &m); + printf("%d\n", ppcm(n, m, n, m)); } -int ppcm(int mult_n,int mult_m,int n,int m) { - if (mult_n < mult_m) { - return ppcm(n+mult_n,mult_m,n,m); - } else if (mult_n > mult_m) { - return ppcm(mult_n,m+mult_m,n,m); - } else { - return mult_n; - } +int ppcm(int mult_n, int mult_m, int n, int m) { + if (mult_n < mult_m) { + return ppcm(n + mult_n, mult_m, n, m); + } else if (mult_n > mult_m) { + return ppcm(mult_n, m + mult_m, n, m); + } else { + return mult_n; + } } diff --git a/source_codes/refactor_with_functions/.gitignore b/source_codes/refactor_with_functions/.gitignore index c5ed9534211ed38b60f9eaf429a5610ce303587f..aa3df14cad2dbe9ba09e3e42e8fcd91ca25e5ec0 100644 --- a/source_codes/refactor_with_functions/.gitignore +++ b/source_codes/refactor_with_functions/.gitignore @@ -11,3 +11,16 @@ tri_select_refactored pgcd_refactored chess_queen_refactored_part ppcm_refactored +nb1er_refactored +palindrome_refactored +tri_select_to_refactor +array_1D_init_find_min_refactored +chess_queen_refactored +ppcm_refactored +pgcd_refactored +factorielle_refactored +tri_select_refactored +chess_queen_refactored_part +eratosthene_refactored +anagramme_refactored +tri_select_refactored_part diff --git a/source_codes/structures/.gitignore b/source_codes/structures/.gitignore index 3b0be08e19b108167024c329497a073d8237d480..87dba4587737c16acdb5eddb8473a5858d43e691 100644 --- a/source_codes/structures/.gitignore +++ b/source_codes/structures/.gitignore @@ -1,2 +1,4 @@ fractions_part fractions +fractions_part +fractions diff --git a/source_codes/tableaux_2d/.gitignore b/source_codes/tableaux_2d/.gitignore index 041e808484d27dc47b02a4d96acddc84abb26b4e..7f3397731fed479f7f5a09e1a7b75d6ef53f853c 100644 --- a/source_codes/tableaux_2d/.gitignore +++ b/source_codes/tableaux_2d/.gitignore @@ -2,3 +2,7 @@ chess_queen_part chess_queen calcul_matrix matrix.o +chess_queen_part +chess_queen +calcul_matrix +matrix.o