文章
43
粉丝
180
获赞
21
访问
196.2k
法一
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n, a[N];
void quick_sort(int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = a[l + r >> 1];
while (i < j)
{
do i ++; while (a[i] < x);
do j --; while (a[j] > x);
if (i < j) swap(a[i], a[j]);
}
quick_sort(l, j); quick_sort(j + 1, r);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> a[i];
quick_sort(0, n - 1);
for (int i = 0; i < n; i ++ ) cout << a[i] << ' ';
return 0;
}
法二
#include <iostream>
#include <algorithm>
using namespace std;
int n;
vector<int> a;
int main()
{
cin >> n;
while (cin >> n) a.push_back(n);
sort(a.begin(), a.end());
for (auto x : a) cout << x << ' ';
return 0;
}
登录后发布评论
暂无评论,来抢沙发