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
| public class QuickSort {
public static void main(String[] args) { int[] numbers = {10, 3, 11, 9, 5};
quickImpl(numbers, 0, numbers.length -1);
Stream.of(numbers).forEach(System.out::println); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
}
private static void quickImpl(int[] arr, int l, int h) { if (l >= h) return; int mid = l; int i = l; int j = h + 1; while (i < j) { while (arr[++i] < arr[mid]) if (i == h) break; while (arr[--j] > arr[mid]) if (j == l) break; if (i > j) break; exch(arr, i, j); } exch(arr, mid, j);
quickImpl(arr, l, j - 1); quickImpl(arr, j + 1, h); }
private static void exch(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
}
|