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
|
public class Quick { public static int[] sort(int[] array,int start,int end) { if(array.length<1||start<0||end>=array.length||start>end) return null; int smallIndex=partition(array,start,end); if(smallIndex>start) sort(array,start,smallIndex-1); if(smallIndex<end) sort(array,smallIndex+1,end); return array; } public static int partition(int[] array,int start ,int end) { int pivot=(int)(start+Math.random()*(end-start+1)); int smallIndex=start-1; swap(array,pivot,end); for(int i=start;i<=end;i++) { if(array[i]<=array[end]) { smallIndex++; if(i>smallIndex) swap(array,i,smallIndex); } } return smallIndex; } public static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; }
}
|
评论