标签:
/*讲解网址 http://wenku.baidu.com/link?url=1xofQsF1043cyk06D7L_OWOJUna67Lx3GNK9Qg6HbADlRH_TMnLx-3EVVlrTVUVqa9c442X576VsAlq8YTp8v7O9P7c_VTiBM7gD_08wYe3 */ package quickSort; /** * 快速排序 * @author Administrator * */ public class QuickSort { void swap(int[] a,int i,int j){ int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } int pos(int[] a,int low,int high){ int temp=a[low],i=low,j=high; while(i<j){ while(i<j){ while((a[j]>=temp)&&(i<j)){ j--; } if(i<j){ swap(a,i,j); i++; } while((a[i]<=temp)&&(i<j)){ i++; } if(i<j){ swap(a,i,j); j--; } } } return i; } void quickSort(int[] a,int low,int high){ int pos; if(low<high){ pos=pos(a,low,high); quickSort(a,low,pos-1); quickSort(a,pos+1,high); } } } package quickSort; /** * 测试类 * @author Administrator * */ public class Test { public static void main(String[] args){ final int N=(int)(Math.random()*100+1); QuickSort q=new QuickSort(); int[] a=new int[N]; for(int j=0;j<N;j++){ a[j]=(int)(Math.random()*100); } System.out.println("排序前:"); for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } q.quickSort(a,0,a.length-1); System.out.println("排序后:"); for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } } }
标签:
原文地址:http://www.cnblogs.com/grow1016/p/4587701.html