mp2i-info/td6/td6.c
2026-01-13 14:30:38 +01:00

50 lines
902 B
C

#include <assert.h>
#include <stdio.h>
#include <string.h>
void printt(int t[], int n) {
printf("[");
for (int i = 0; i < n; i++) {
printf("%d,", t[i]);
}
printf("]\n");
}
void swap(int t[], int i, int j) {
int tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
void quicksort(int t[], int n) {
if (n <= 1) {
return;
}
int a = 0;
int b = n - 1;
int p = t[n - 1];
while (a < b) {
if (t[a] <= p)
a++;
else if (t[b] >= p)
b--;
else {
swap(t, a, b);
}
}
swap(t, b, n - 1);
quicksort(t, b);
quicksort(t + b + 1, n - b - 1);
}
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[n];
memcpy(t1, t, n * sizeof(int));
quicksort(t1, n);
assert(memcmp(t1, sorted, n * sizeof(int)) == 0);
}