1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include<iostream> using std::cin; using std::cout; using std::endl; void swap(int a[],int i,int j){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } int partition(int a[],int left,int right){ int l=left; int r=right+1; while(true){ while(a[++l]<a[left] && l<r); while(a[--r]>a[left]); if(l>=r) break; swap(a,l,r); } swap(a,left,r); return r; } void qsort(int a[],int left,int right){ if(left<right){ int mid=partition(a,left,right); qsort(a,left,mid-1); qsort(a,mid+1,right); } } void print(int a[],int size){ for(int i=0;i<size;i++) cout<<a[i]<<" "; cout<<endl; } int main(){ cout<<"请输入要排序的数组大小:"<<endl; int count=0; cin>>count; int *a=new int[count]; cout<<"请输入数组:"<<endl; for(int i=0;i<count;i++){ cin>>a[i]; } qsort(a,0,count-1); print(a,count); return 0; }
|
近期评论