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 52 53
|
/** * 完美的快排 * Created by xwf on 17/11/29. * http://developer.51cto.com/art/201403/430986.htm */ public class QuickSort { private int[] a;
public int[] getA() { return a; }
public void setA(int[] a) { this.a = a; }
public void sort(int left, int right) { int i,j,t,temp; if(left>right) return; temp = a[left]; //基准 i=left; j=right; while (i!=j) { while (a[j] >= temp && i<j) { j--; } while (a[i] <= temp && i<j) { i++; } if(i<j) { t = a[i]; a[i] = a[j]; a[j] = t; }
} // 基准书归为 a[left] = a[i]; a[i] = temp; sort(left,i-1); sort(i+1,right); }
public static void main(String[] args) { int array[] = {6,1,74,26,78}; QuickSort quickSort = new QuickSort(); quickSort.setA(array); quickSort.sort(0,array.length-1); for(int value : quickSort.getA()) { System.out.printf(value+" "); } } }
|
近期评论