文章
19
粉丝
0
获赞
125
访问
3.0k
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N];
bool cmp(int x, int y) {
// 奇数在前,偶数在后
if (x % 2 != y % 2) {
return x % 2 > y % 2; // 奇数优先
}
// 同奇偶性时,按从小到大排序
return x < y;
}
void quickSort(int l, int r) {
if (l >= r) return;
int x = a[l], i = l, j = r;
while (i < j) {
while (i < j && !cmp(a[j], x)) j--; // !cmp(a[j], x) 表示 a[j] 不应该排在 x 的前面。
//如果 !cmp(a[j], x) 为 true,说明 a[j] 应该排在x的后面,因此需要继续向左移动j,直到找到应该排在 x 前面的元素。
if (i < j) a[i++] = a[j];
while (i < j && cmp(a[i], x)) i++;// 如果 cmp(a[j], x) 返回 true,表示 a[j] 应该排在 x 的前面。
//如果 cmp(a[j], x) 返回 false,表示 a[j] 不应该排在 x 的前面。
if (i < j) a[j--] = a[i];
}
a[i] = x;
quickSort(l, i - 1);
quickSort(i + 1, r);
}
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
quickSort(0 , n - 1);
for(int i =...
登录后发布评论
暂无评论,来抢沙发