mp2i-info/td6/exo1.c
2026-01-06 17:51:44 +01:00

58 lines
1.1 KiB
C

#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;
}