debug c + td6 exo1
This commit is contained in:
parent
042f126730
commit
b4f83ba2dc
13
.zed/debug.json
Normal file
13
.zed/debug.json
Normal file
@ -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",
|
||||
},
|
||||
]
|
||||
57
td6/exo1.c
Normal file
57
td6/exo1.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user