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
| public void swap(int[] arr, int left, int right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; }
public int split(int[] arr, int left, int right) { int i = left; int temp = arr[left]; for (int j = left+1; j <= right; ++j) { if (arr[j] < temp) { swap(arr, j, ++i); } } swap(arr, left, i); return i; }
public int partition(int[] arr, int left, int right) { int temp = arr[left]; int i = left; int j = right; while (i < j) { while (i < j && arr[j] >= temp) --j; while (i < j && arr[i] <= temp) ++i; swap(arr, i, j); if (i == j) swap(arr, left, j); } return j; }
public void quickSort(int[] arr, int left, int right) { int i = partition(arr, left, right); if (left < i-1) quickSort(arr, left, i-1); if (i+1 < right) quickSort(arr, i+1, right); }
|