快速排序 c++

 

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1064

 

#include<iostream> #include<cstdio> #include<algorithm> using namespace std; void swap(int &a,int &b){ int t=a; a=b; b=t; } int split(int *A ,int low,int high){ int i,k,x=A[low]; i=low; for(k=low+1;k<=high;++k) if(A[k]<x){ i++; if(i!=k)swap(A[k],A[i]); } swap(A[low],A[i]); return i; } void q_sort(int *A,int low,int high){ int k; if(low<high){ k=split(A,low,high); q_sort(A,low,k-1); q_sort(A,k+1,high); } } int cmp(const void *a,const void *b){ return (*(int*)a)-(*(int*)b); } int main(int argc, char *argv[]) { int n,i,A[100005]; cin>>n; for(i=0;i<n;++i){ scanf("%d",&A[i]); } // q_sort(A,0,n-1); qsort(A,n,sizeof(int),cmp); for(i=0;i<n;++i) if(i==n-1)printf("%d",A[n-1]); else printf("%d ",A[i]); return 0; }

你可能感兴趣的:(C++)