// Comment Quick Sort public static void quick_sort(int[] a) { int len = a.length; if (a == null || len == 0) throw new IllegalArgumentException("Null/Invalid Input"); quick_sortUtil(a, 0, len - 1); } private static void quick_sortUtil(int[] a, int l, int r) { if (l > r) return; int p = pivot(a, l, r); quick_sortUtil(a, l, p - 1); quick_sortUtil(a, p + 1, r); } private static int pivot(int[] a, int l, int r) { int x = a[r], j, i; for (i = l, j = l; i < r && j < r; i++) { if (a[i] <= x) { swapArr(a, i, j); j++; } } swapArr(a, j, r); return j; } private static void swapArr(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
Thursday, September 4, 2014
Quick sort in Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment