qsort

qsort
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
inline ll read(){
    ll x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) ch=='-'&(f=-1),ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x*f;
}
int n;
const int N=1<<20;
int a[N];
signed main(){
    n=read();
    for(register int i=1;i<=n;i++) a[i]=read();
    sort(a+1,a+n+1);
    for(register int i=1;i<=n;i++) cout<<a[i]<<' ';
    return 0;
}
快排【滑稽】
#include <iostream>
using namespace std;
typedef long long ll;
inline ll read(){
    ll x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) ch=='-'&(f=-1),ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x*f;
}
int n;
const int N=1<<20;
int a[N];
template<typename T>inline void qsort(T a[],int l,int r){
    if(l>=r) return ;
    T p=a[l];
    int i=l,j=r;
    while(i!=j){
        while(i<j and a[j]>=p) j--; swap(a[i],a[j]);
        while(i<j and a[i]<=p) i++; swap(a[i],a[j]);
    }
    qsort(a,l,i-1); qsort(a,i+1,r);
}
signed main(){
    n=read();
    for(register int i=1;i<=n;i++) a[i]=read();
    qsort(a,1,n);
    for(register int i=1;i<=n;i++) cout<<a[i]<<' ';
    return 0;
}

 

上一篇:qsort函数(C版)


下一篇:[OpenGL]未来视觉2-Android摄像头帧采集