快速排序
平时写题用sort就行了,但是如果要求排序就中间结果诸类的题,还是有点用!
听说long long ago,高中组的比赛只要会打sort代码就能得分(当时不允许用STL)
我也想比赛考这样的题……
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N=1e6+5; 4 int n,q[N]; 5 void quick_sort(int q[],int l,int r){ 6 if(l>=r) return; 7 int x=q[l+r>>1]; 8 int i=l-1,j=r+1; 9 while(i<j){ 10 do i++; while(q[i]<x); 11 do j--; while(q[j]>x); 12 if(i<j) swap(q[i],q[j]); 13 } 14 quick_sort(q,l,j); 15 quick_sort(q,j+1,r); 16 } 17 18 int main(){ 19 ios::sync_with_stdio(false); 20 cin.tie(0); 21 cin>>n; 22 for(int i=0;i<n;i++) 23 cin>>q[i]; 24 quick_sort(q,0,n-1); 25 for(int i=0;i<n;i++) 26 cout<<q[i]<<' '; 27 return 0; 28 }