From ee884b145b65da657d7ba666a426985ecfb15314 Mon Sep 17 00:00:00 2001
From: Orestis <orestis.malaspinas@pm.me>
Date: Mon, 10 Jan 2022 23:50:45 +0100
Subject: [PATCH] ajout recherche sequentielle et dichotomique

---
 slides/cours_13.md                            | 118 +++++++++++++++++-
 source_codes/complexity/sum.c                 |   2 +-
 source_codes/loops_tests/.gitignore           |   5 +
 source_codes/pointeurs/.gitignore             |   1 +
 source_codes/recursivity/rec_fibonacci.c      |   9 ++
 source_codes/recursivity/rec_ppcm.c           |  30 ++---
 .../refactor_with_functions/.gitignore        |  13 ++
 source_codes/structures/.gitignore            |   2 +
 source_codes/tableaux_2d/.gitignore           |   4 +
 9 files changed, 167 insertions(+), 17 deletions(-)

diff --git a/slides/cours_13.md b/slides/cours_13.md
index c1edd14..0c6990f 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 4a9780a..44e9303 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 0a63c70..244441b 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 7bf15f4..d599336 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 eb134a8..9395206 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 ef505ee..bc4ba9a 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 c5ed953..aa3df14 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 3b0be08..87dba45 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 041e808..7f33977 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
-- 
GitLab