快速排序(i和j交互法)
/** * @author wxl * @Title: QuickSort * @ProjectName test * @Description: TODO * @date 2019-4-19 18:24 */ public class QuickSort { public static void main(String[] args) { int[] a={10,9,7,3,3,8,6}; for(int i=0;i<a.length;i++){ System.out.print(a[i]+ " "); } System.out.println(); sort(a,0,a.length-1); for(int i=0;i<a.length;i++){ System.out.print(a[i]+ " "); } System.out.println(); } public static void sort(int[] a,int l,int r){ if(l<r){ int x=a[l],i=l,j=r; while(i<j){ while(i<j&&a[j]>=x){ j--; } while(i<j&&a[i]<=x){ i++; } if(i<j){ a[i]^=a[j]; a[j]^=a[i]; a[i]^=a[j]; } } a[l]=a[i]; a[i]=x; sort(a,l,i-1); sort(a,i+1,r); } } }