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 65 66 67 68 69 70
| import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) { int[] array = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48};
quickSort(array);
System.out.println(Arrays.toString(array)); }
public static void quickSort(int[] array) { quickSort(array, 0, array.length - 1); }
private static void quickSort(int[] array, int left, int right) { if (array == null || left >= right || array.length <= 1) { return; } int mid = partition(array, left, right); quickSort(array, left, mid); quickSort(array, mid + 1, right); }
private static int partition(int[] array, int left, int right) { int temp = array[left]; while (right > left) { while (temp <= array[right] && left < right) { --right; } if (left < right) { array[left] = array[right]; ++left; } while (temp >= array[left] && left < right) { ++left; } if (left < right) { array[right] = array[left]; --right; } } array[left] = temp; return left; } }
|