例题:快速排序模板:
AC代码:
源码:
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int n;
int q[N];void quick_sort(int q[], int l, int r) {if (l >= r)return ;int x = q[l], i = l - 1, j = r + 1;while (i < j) {doi++;while (q[i] < x);doj--;while (q[j] > x);if (i < j)swap(q[i], q[j]);}quick_sort(q, l, j);quick_sort(q, j + 1, r);
}int main() {scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%d", &q[i]);quick_sort(q, 0, n - 1);for (int i = 0; i < n; i++)printf("%d ", q[i]);return 0;
}
(sort)快排:
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>vector<int>v;
int n,nn;
int main()
{cin>>n;for(int i=0;i<n;i++){cin>>nn;v.push_back(nn);}sort(v.begin(),v.end());for(int i=0;i<v.size();i++){cout<<v[i]<<" ";}return 0;
}
#include <bits/stdc++.h>using namespace std;const int N = 1E6 + 3;int a[N],b[N],idx;int main(void) {int n;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];}sort(a,a+n);for (int i = 0; i < n; i++) {cout << a[i] << " ";}return 0;
}
总结:其实直接用c++的sort即可,没必要直接写出来源码。感谢大家的观看!!!