BUPT-homework/semester1/pset9/3-sort.c

36 lines
593 B
C
Raw Normal View History

2021-11-25 16:13:12 +08:00
#include <stdio.h>
void swap(int *arr, int l, int r);
int main(void) {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
/* swap(arr, 0, 1); */
// Bubble sort
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[j] % 2 == 1 && arr[j + 1] % 2 == 0) {
swap(arr, j, j + 1);
}
}
}
for (int i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}
printf("%d\n", arr[n - 1]);
return 0;
}
void swap(int *arr, int l, int r) {
int tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
}