public static void sort(int[] a) { int N = a.length; for(int i = 0; i < N; i++) { int min = i; for(int j = i+1; a[j] < a[min]; j++) min = j; int tmp = a[i]; a[i] = a[min]; a[min] = tmp; } }
public class Quick { private static int partition(int[] a, int lo, int hi) { int i = lo, j = hi + 1; // 左右扫描指针 int v = a[lo]; // 切分元素 while(true) { while(a[++i] < v) { if (i == hi) break; } while(v < a[--j]) { if (j == lo) break; } if (i >= j) break; int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } int tmp = a[lo]; a[lo] = a[j]; a[j] = tmp; return j; }
public static void sort(int[] a) { StdRandom.shuffle(a); sort(a, 0, a.length - 1); }
private static void sort(int[] a, int lo, int hi) { if (hi <= lo) return; int j = partition(a, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); } }