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 54 55 56 57 58 59 60 61 62 63
| public static void quickSort(int[] arr,int left,int right){ if (left<right) { int i = AdjustArray(arr,left,right); quickSort(arr, left, i-1); quickSort(arr, i+1, right); } } public static int AdjustArray(int[] arr,int left, int right) { int i = left,j=right; int x = arr[left]; while (i<j) { while (i<j&&arr[j]>=x) j--; if (i<j) arr[i++]=arr[j]; while (i<j&&arr[i]<x) i++; if (i<j) arr[j--]=arr[i]; } arr[i]=x; return i; } public void quickSort2(int[] arr,int left,int right) { if (left<right) { int i = left,j=right,x=arr[left]; while (i<j){ while (i<j&&arr[j]>=x) j--; if(i<j) arr[i++] =arr[j]; while (i<j&&arr[i]<x) i++; if(i<j) arr[j--] = arr[i]; } arr[i]=x; quickSort2(arr, left, i-1); quickSort2(arr, i+1, right); } }
public static void main(String[] args) { int[] arrTest = {2,5,1,9,3,7,4,6,8}; classSort m = new classSort(); m.quickSort2(arrTest,0,arrTest.length-1); System.out.println(Arrays.toString(arrTest)); }
}
|