RVBTCC/test/sort.c

47 lines
1.0 KiB
C
Raw Normal View History

2024-12-07 04:24:00 +00:00
int printf(char* format, ...);
int scanf(char* format, ...);
2024-12-08 07:41:38 +00:00
int exit(int status);
void assert_eq(int expected, int actual) {
if (expected != actual) {
printf("expected: %d, actual: %d\n", expected, actual);
exit(1);
}
}
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
2024-11-17 02:39:58 +00:00
void sort(int a[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
2024-12-08 07:41:38 +00:00
swap(&a[i], &a[j]);
assert_eq(i - j, &a[i] - &a[j]);
assert_eq(j - i, &a[j] - &a[i]);
assert_eq(a[i], *(a + i));
assert_eq(i[a], *(i + a));
assert_eq(a[j - i], *(a + (j - i)));
assert_eq(j[a - i], *(j + (a - i)));
2024-11-17 02:39:58 +00:00
}
}
}
}
2024-12-08 07:41:38 +00:00
int a[100];
int n;
2024-11-17 02:39:58 +00:00
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, n);
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}