public class QuickSort {
public int[] sort(int[] array) { doSort(array, 0, array.length - 1); return array; }
private void doSort(int[] array, int left, int right) { if (left >= right) { return; } int pivot = partition(array, left, right); doSort(array, left, pivot - 1); doSort(array, pivot, right); }
private int partition(int[] array, int left, int right) { int key = array[right]; int i = left - 1; for (int j = left; j <= right - 1; j++) { if (array[j] <= key) { i++; swap(array, i, j); } } swap(array, i + 1, right); return i + 1; }
static boolean swap(int[] array, int idx, int idy) { int swap = array[idx]; array[idx] = array[idy]; array[idy] = swap; return true; }
public static void main(String[] args) { int[] array = {3, 4, 1, 32, 0, 1, 5, 12, 2, 5, 7, 8, 9, 2, 44, 111, 5};
QuickSort quickSort = new QuickSort(); quickSort.sort(array);
Arrays.stream(array).forEach(System.out::println);
} }
|