快排采用分治的方法,
通常以第一个数为基准,
从右边找比它小的数交换位置,
然后从左边找比它大的数交换位置,
直到基准左边的数小于它,右边的数大于它。
private static void quicksort(int[] a, int l, int r) { if(l<r){ int i=l,j=r,x=a[l]; while(i<j){ //寻找右边第一个小于x的并交换 while (a[j]>x) { j--; } if(a[j]<=x){ a[i]=a[j]; a[j]=x; i++; } //寻找左边第一个大于x与x交换 while (a[i]<x){ i++; } if(a[i]>=x){ a[j]=a[i]; a[i]=x; j--; } } quicksort(a,l,i-1); quicksort(a,j+1,r); } }