排序 快排&归并&基数
#include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<queue> #include<set> #define maxint (2147483647) #define l(a) ((a)<<1) #define r(a) ((a)<<1|1) #define b(a) (2<<(a)) #define rep(i,a,b) for(int i=a;i<=(b);i++) #define clr(a) memset(a,0,sizeof(a)) typedef long long ll; using namespace std; int readint(){ int t=0,f=1;char c=getchar(); while(!isdigit(c)){ if(c=='-') f=-1; c=getchar(); } while(isdigit(c)){ t=(t<<3)+(t<<1)+c-'0'; c=getchar(); } return t*f; } ll readll(){ ll t=0ll,f=1ll;char c=getchar(); while(!isdigit(c)){ if(c=='-') f=-1ll; c=getchar(); } while(isdigit(c)){ t=(t<<3ll)+(t<<1ll)+ll(c-'0'); c=getchar(); } return t*f; } const int maxn=100009; int n,t,a[maxn],b[maxn]; void merge_sort(int l,int r){ if(l==r) return; int mid=(l+r)>>1,L=l,R=mid+1; merge_sort(l,mid); merge_sort(mid+1,r); do{ if(L>mid) b[L+R-mid-1]=a[R++]; else if(R>r) b[L+R-mid-1]=a[L++]; else if(a[L]<a[R]) b[L+R-mid-1]=a[L++]; else b[L+R-mid-1]=a[R++]; }while(L<=mid||R<=r); rep(i,l,r) a[i]=b[i]; } int main(){ //freopen("#intput.txt","r",stdin); //freopen("#output.txt","w",stdout); n=readint(); rep(i,1,n) a[i]=readint(); merge_sort(1,n); rep(i,1,n){ printf("%d",a[i]); putchar(i==n?'\n':' '); } //fclose(stdin); //fclose(stdout); return 0; }
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<queue> #include<set> #define maxint (2147483647) #define l(a) ((a)<<1) #define r(a) ((a)<<1|1) #define b(a) (2<<(a)) #define rep(i,a,b) for(int i=a;i<=(b);i++) #define clr(a) memset(a,0,sizeof(a)) typedef long long ll; using namespace std; int readint(){ int t=0,f=1;char c=getchar(); while(!isdigit(c)){ if(c=='-') f=-1; c=getchar(); } while(isdigit(c)){ t=(t<<3)+(t<<1)+c-'0'; c=getchar(); } return t*f; } int n,a[100009]; void qsort(int L,int R){ int mid=a[(L+R)>>1],l=L,r=R; while(l<=r){ while(a[l]<mid) l++; while(a[r]>mid) r--; if(l<=r) swap(a[l++],a[r--]); } if(L<r) qsort(L,r); if(l<R) qsort(l,R); } int main(){ //freopen("#input.txt","r",stdin); //freopen("#output.txt","w",stdout); n=readint(); rep(i,0,n-1) a[i]=readint(); qsort(0,n-1); rep(i,0,n-1){ printf("%d",a[i]); if(i!=n-1) putchar(' '); } //fclose(stdin); //fclose(stdout); return 0; }
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<ctime> 5 #include<iostream> 6 #include<algorithm> 7 #include<queue> 8 #include<set> 9 #define inf (0x7fffffff) 10 #define l(a) ((a)<<1) 11 #define r(a) ((a)<<1|1) 12 #define b(a) (1<<(a)) 13 #define rep(i,a,b) for(int i=a;i<=(b);i++) 14 #define clr(a) memset(a,0,sizeof(a)) 15 typedef long long ll; 16 typedef unsigned long long ull; 17 using namespace std; 18 int readint(){ 19 int t=0,f=1;char c=getchar(); 20 while(!isdigit(c)){ 21 if(c=='-') f=-1; 22 c=getchar(); 23 } 24 while(isdigit(c)){ 25 t=(t<<3)+(t<<1)+c-'0'; 26 c=getchar(); 27 } 28 return t*f; 29 } 30 const int maxn=100009; 31 int n,mx,l,x[maxn],cnt[10],a[10][maxn]; 32 void bsort(){ 33 int t=1; 34 rep(i,1,l){ 35 clr(cnt); 36 rep(j,1,n){ 37 int d=x[j]%(t*10)/t; 38 a[d][++cnt[d]]=x[j]; 39 } 40 int tmp=0; 41 rep(j,0,9) rep(k,1,cnt[j]) x[++tmp]=a[j][k]; 42 //rep(j,1,n) printf("%d ",x[j]);puts(""); 43 t*=10; 44 } 45 rep(i,1,n){ 46 printf("%d",x[i]); 47 putchar(i==n?'\n':' '); 48 } 49 } 50 int main(){ 51 //freopen("#input.txt","r",stdin); 52 //freopen("#output.txt","w",stdout); 53 n=readint(); 54 rep(i,1,n){ 55 x[i]=readint();mx=max(mx,x[i]); 56 } 57 for(l=0;mx;mx/=10) l++; 58 bsort(); 59 //fclose(stdin); 60 //fclose(stdout); 61 return 0; 62 }