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
| import java.util.*;
public class QuickSort { public static void main(String[] args) { int[] arr = {9,4,6,8,1,5,3}; System.out.println(Arrays.toString(arr)); quickSort(arr); System.out.println(Arrays.toString(arr)); }
public static void quickSort(int[] arr) { recurPartiton(arr, 0, arr.length-1); }
public static void recurPartiton(int[] arr, int start,int end) { if (end-start<1) { return; } int part = partArr(arr, start, end); if (part == start) { recurPartiton(arr, part+1, end); } else if(part == end) { recurPartiton(arr, start, end-1); } else { recurPartiton(arr, start, part-1); recurPartiton(arr, part+1, end); } }
public static int partArr(int[] arr, int start, int end) { int base = arr[end]; int n = start; for (int i = start; i < end; i++) { if(arr[i] < base) { if (i != n){ exchangeE(arr, i, n); } n++; } } exchangeE(arr, n, end); return n; } public static void exchangeE(int[] arr, int index1, int index2){ int temp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = temp; } }
|