From 1a07fc21f63a8ee9b29864f85196ec213eab6834 Mon Sep 17 00:00:00 2001 From: Arkitu Date: Mon, 12 Jan 2026 21:57:00 +0100 Subject: [PATCH] td5 exo1 et exo2 --- .zed/debug.json | 11 +++++ td5/exo1.c | 10 ----- td5/{ => ocaml}/dune-project | 0 td5/{ => ocaml}/lib/dune | 0 td5/{ => ocaml}/lib/lib.ml | 0 td5/{ => ocaml}/td5.opam | 0 td5/{ => ocaml}/test/dune | 0 td5/{ => ocaml}/test/test.ml | 0 td5/td5.c | 79 ++++++++++++++++++++++++++++++++++++ 9 files changed, 90 insertions(+), 10 deletions(-) delete mode 100644 td5/exo1.c rename td5/{ => ocaml}/dune-project (100%) rename td5/{ => ocaml}/lib/dune (100%) rename td5/{ => ocaml}/lib/lib.ml (100%) rename td5/{ => ocaml}/td5.opam (100%) rename td5/{ => ocaml}/test/dune (100%) rename td5/{ => ocaml}/test/test.ml (100%) create mode 100644 td5/td5.c diff --git a/.zed/debug.json b/.zed/debug.json index 8849f8d..cff2839 100644 --- a/.zed/debug.json +++ b/.zed/debug.json @@ -10,4 +10,15 @@ "request": "launch", "adapter": "CodeLLDB", }, + { + "label": "Debug td5", + "build": { + "command": "gcc", + "args": ["-o", "./exe", "-lm", "-g", "td5/td5.c"], + "cwd": "$ZED_WORKTREE_ROOT", + }, + "program": "$ZED_WORKTREE_ROOT/exe", + "request": "launch", + "adapter": "CodeLLDB", + }, ] diff --git a/td5/exo1.c b/td5/exo1.c deleted file mode 100644 index aa803fc..0000000 --- a/td5/exo1.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "stdio.h" -#include "stdlib.h" - -void intertion_sort(int t[], int n) { - for (int i = 1; i < n; i++) { - int j = 0; - while (j < i && t[j] <= t[i]) - j++; - } -} diff --git a/td5/dune-project b/td5/ocaml/dune-project similarity index 100% rename from td5/dune-project rename to td5/ocaml/dune-project diff --git a/td5/lib/dune b/td5/ocaml/lib/dune similarity index 100% rename from td5/lib/dune rename to td5/ocaml/lib/dune diff --git a/td5/lib/lib.ml b/td5/ocaml/lib/lib.ml similarity index 100% rename from td5/lib/lib.ml rename to td5/ocaml/lib/lib.ml diff --git a/td5/td5.opam b/td5/ocaml/td5.opam similarity index 100% rename from td5/td5.opam rename to td5/ocaml/td5.opam diff --git a/td5/test/dune b/td5/ocaml/test/dune similarity index 100% rename from td5/test/dune rename to td5/ocaml/test/dune diff --git a/td5/test/test.ml b/td5/ocaml/test/test.ml similarity index 100% rename from td5/test/test.ml rename to td5/ocaml/test/test.ml diff --git a/td5/td5.c b/td5/td5.c new file mode 100644 index 0000000..21b05d4 --- /dev/null +++ b/td5/td5.c @@ -0,0 +1,79 @@ +#include +#include +#include + +void printt(int t[], int n) { + printf("["); + for (int i = 0; i < n; i++) { + printf("%d,", t[i]); + } + printf("]\n"); +} + +// Exercice 1 + +void insertion_sort(int t[], int n) { + for (int i = 0; i < n; i++) { + int x = t[i]; + int j = i - 1; + while (j >= 0 && t[j] > x) { + t[j + 1] = t[j]; + j--; + } + t[j + 1] = x; + } +} + +void selection_sort(int t[], int n) { + for (int i = 0; i < n; i++) { + int min = t[i]; + int min_i = i; + for (int j = i; j < n; j++) { + if (t[j] < min) { + min = t[j]; + min_i = j; + } + } + t[min_i] = t[i]; + t[i] = min; + } +} + +void cocktail_sort(int t[], int n) { + for (int i = 0; i < n / 2; i++) { + for (int j = i; j < n - i - 1; j++) { + if (t[j] > t[j + 1]) { + int tmp = t[j + 1]; + t[j + 1] = t[j]; + t[j] = tmp; + } + } + for (int j = n - i - 2; j >= i; j--) { + if (t[j] > t[j + 1]) { + int tmp = t[j + 1]; + t[j + 1] = t[j]; + t[j] = tmp; + } + } + } +} + +const int t[] = {5, 3, 9, 0, 5, 0}; +const int sorted[] = {0, 0, 3, 5, 5, 9}; +const int n = 6; + +int main() { + int t1[6]; + memcpy(t1, t, n * sizeof(int)); + insertion_sort(t1, n); + assert(memcmp(t1, sorted, n * sizeof(int)) == 0); + + memcpy(t1, t, n * sizeof(int)); + selection_sort(t1, n); + assert(memcmp(t1, sorted, n * sizeof(int)) == 0); + + memcpy(t1, t, n * sizeof(int)); + cocktail_sort(t1, n); + printt(t1, n); + assert(memcmp(t1, sorted, n * sizeof(int)) == 0); +}