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
| public static void quickSort(int[] num, int begin, int end){ if(begin<end){ int mid=pivotIndex(num,begin,end); quickSort(num,begin,mid-1); quickSort(num,mid+1,end); } } public static int pivotIndex(int[] num, int begin, int end){ int pivot=num[begin]; while(begin<end){ while(begin<end && pivot<=num[end]) { end--; } swap(num,begin,end); while(begin<end && pivot>=num[begin]) { begin++; } swap(num,begin,end); } num[begin]=pivot; return begin; } public static void swap(int[] num, int i, int j){ int tmp=num[i]; num[i]=num[j]; num[j]=tmp; }
|