diff --git a/.zed/debug.json b/.zed/debug.json new file mode 100644 index 0000000..8849f8d --- /dev/null +++ b/.zed/debug.json @@ -0,0 +1,13 @@ +[ + { + "label": "Debug td6 exo1", + "build": { + "command": "gcc", + "args": ["-o", "./exe", "-lm", "-g", "td6/exo1.c"], + "cwd": "$ZED_WORKTREE_ROOT", + }, + "program": "$ZED_WORKTREE_ROOT/exe", + "request": "launch", + "adapter": "CodeLLDB", + }, +] diff --git a/td6/exo1.c b/td6/exo1.c new file mode 100644 index 0000000..c3704e0 --- /dev/null +++ b/td6/exo1.c @@ -0,0 +1,57 @@ +#include +#include + +void print_t(int t[], int len) { + printf("["); + for (int i = 0; i < len; i++) { + printf("%d,", t[i]); + } + printf("]\n"); +} + +void quicksort(int t[], int len) { + if (len < 2) { + return; + } + int ip = len / 2; + int p = t[ip]; + int a = 0; + int b = len - 1; + + while (a < b) { + while (t[a] < p && a < b) { + a++; + if (a >= ip) { + int tmp = t[a]; + t[a] = t[ip]; + t[ip] = tmp; + ip++; + a--; + } + } + while (t[b] > p && a < b) { + b--; + if (b <= ip) { + int tmp = t[b]; + t[b] = t[ip]; + t[ip] = tmp; + ip--; + b++; + } + } + int tmp = t[a]; + t[a] = t[b]; + t[b] = tmp; + a++; + b--; + } + quicksort(t, ip); + quicksort(t + ip + 1, len - ip - 1); +} + +int main() { + int t[] = {0, -7, 4, -2, 2, -3}; + quicksort(t, 6); + print_t(t, 6); + return 0; +}