魯班Java架構(gòu)VIP
public static void quickSortPartition(int[] array, int left, int right) {
? ? ? ?if (right > left) {
? ? ? ? ? ?// 將 [left - right] 區(qū)域分成了
? ? ? ? ? ?// [left, pivotIndex - 1] 和 [pivotIndex + 1, right] 兩個(gè)
? ? ? ? ? ?int pivotIndex = partition(array, left, right);
? ? ? ? ? ?quickSortPartition(array, left, pivotIndex - 1);
? ? ? ? ? ?quickSortPartition(array, pivotIndex + 1, right);
? ? ? ?}
? ?}
? ?public static int partition(int[] array, int left, int right) {
? ? ? ?// 挑選最右側(cè)的作為基準(zhǔn)值
? ? ? ?int pivotValue = array[right];
? ? ? ?int storeIndex = left;
? ? ? ?for (int i = left; i < right; i++) {
? ? ? ? ? ?if (array[i] <= pivotValue) {
? ? ? ? ? ? ? ?swap(array, i, storeIndex);
? ? ? ? ? ? ? ?storeIndex += 1;
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?swap(array, storeIndex, right);
? ? ? ?return storeIndex;
? ?}
? ?private static void swap(int[] x, int a, int b) {
? ? ? ?int t = x[a];
? ? ? ?x[a] = x[b];
? ? ? ?x[b] = t;
? ?}
