P1177 【模板】快速排序
P1177 【模板】快速排序
代码如下:
#include<bits/stdc++.h> using namespace std; #define mm(a,x) memset(a,x,sizeof a) #define mk make_pair #define ll long long #define pii pair<int,int> #define inf 0x3f3f3f3f #define lowbit(x) (x) & (-x) const int N = 1e5 + 10; int n; int a[N]; void quick_sort(int a[],int l,int r){ if(l >= r) return ; int x = a[l + r >> 1]; int i = l - 1, j = r + 1;//取边界 while(i < j){ do i ++; while(a[i] < x);//当前数值小于a[mid],i ++; do j --; while(a[j] > x);//当前数值大于a[mid],j --; if(i < j) swap(a[i],a[j]);//交换下标为i,j的数值 } quick_sort(a,l,j);quick_sort(a,j + 1,r); } int main() { cin >> n; for(int i = 1; i <= n; i ++ ) cin >> a[i]; quick_sort(a,1,n); for(int i = 1; i <= n; i ++ ) cout<<a[i]<<" "; return 0; }