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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public static void quickSort(int[] array) { quickSort(array, 0, array.length - 1); }
private static void quickSort(int[] array, int left, int right) { if (left < right) { int index = quickSortOne(array, left, right); quickSort(array, left, index - 1); quickSort(array, index + 1, right); } }
private static int quickSortOne(int[] array, int left, int right) { int pivot = array[left]; int start = left; int end = right; while (start < end) { while (array[end] >= pivot && start < end) { end--; } while (array[start] <= pivot && start < end) { start++; } swap(array, start, end); } swap(array, left, start); return start; }
private static void swap(int[] array, int left, int right) { int temp = array[left]; array[left] = array[right]; array[right] = temp; }
|