| 
001002
 003
 004
 005
 006
 007
 008
 009
 010
 011
 012
 013
 014
 015
 016
 017
 018
 019
 020
 021
 022
 023
 024
 025
 026
 027
 028
 029
 030
 031
 032
 033
 034
 035
 036
 037
 038
 039
 040
 041
 042
 043
 044
 045
 046
 047
 048
 049
 050
 051
 052
 053
 054
 055
 056
 057
 058
 059
 060
 061
 062
 063
 064
 065
 066
 067
 068
 069
 070
 071
 072
 073
 074
 075
 076
 077
 078
 079
 080
 081
 082
 083
 084
 085
 086
 087
 088
 089
 090
 091
 092
 093
 094
 095
 096
 097
 098
 099
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 
 | // Exercise 2.3.22 (Solution published at http://algs4.cs.princeton.edu/)
package algs23;
import stdlib.*;
/* ***********************************************************************
 *  Compilation:  javac QuickX.java
 *  Execution:    java QuickX N
 *
 *  Uses the Bentley-McIlroy 3-way partitioning scheme,
 *  chooses the partitioning element using Tukey's ninther,
 *  and cuts off to insertion sort.
 *
 *  Reference: Engineering a Sort Function by Jon L. Bentley
 *  and M. Douglas McIlroy. Softwae-Practice and Experience,
 *  Vol. 23 (11), 1249-1265 (November 1993).
 *
 *************************************************************************/
public class XQuickX {
  private static final int CUTOFF = 8;  // cutoff to insertion sort, must be >= 1
  public static <T extends Comparable<? super T>> void sort(T[] a) {
    sort(a, 0, a.length - 1);
  }
  private static <T extends Comparable<? super T>> void sort(T[] a, int lo, int hi) {
    int N = hi - lo + 1;
    // cutoff to insertion sort
    if (N <= CUTOFF) {
      insertionSort(a, lo, hi);
      return;
    }
    // use median-of-3 as partitioning element
    else if (N <= 40) {
      int m = median3(a, lo, lo + N/2, hi);
      exch(a, m, lo);
    }
    // use Tukey ninther as partitioning element
    else  {
      int eps = N/8;
      int mid = lo + N/2;
      int m1 = median3(a, lo, lo + eps, lo + eps + eps);
      int m2 = median3(a, mid - eps, mid, mid + eps);
      int m3 = median3(a, hi - eps - eps, hi - eps, hi);
      int ninther = median3(a, m1, m2, m3);
      exch(a, ninther, lo);
    }
    // Bentley-McIlroy 3-way partitioning
    int i = lo, j = hi+1;
    int p = lo, q = hi+1;
    while (true) {
      T v = a[lo];
      while (less(a[++i], v))
        if (i == hi) break;
      while (less(v, a[--j]))
        if (j == lo) break;
      if (i >= j) break;
      exch(a, i, j);
      if (eq(a[i], v)) exch(a, ++p, i);
      if (eq(a[j], v)) exch(a, --q, j);
    }
    exch(a, lo, j);
    i = j + 1;
    j = j - 1;
    for (int k = lo+1; k <= p; k++) exch(a, k, j--);
    for (int k = hi  ; k >= q; k--) exch(a, k, i++);
    sort(a, lo, j);
    sort(a, i, hi);
  }
  // sort from a[lo] to a[hi] using insertion sort
  private static <T extends Comparable<? super T>> void insertionSort(T[] a, int lo, int hi) {
    for (int i = lo; i <= hi; i++)
      for (int j = i; j > lo && less(a[j], a[j-1]); j--)
        exch(a, j, j-1);
  }
  // return the index of the median element among a[i], a[j], and a[k]
  private static <T extends Comparable<? super T>> int median3(T[] a, int i, int j, int k) {
    return (less(a[i], a[j]) ?
        (less(a[j], a[k]) ? j : less(a[i], a[k]) ? k : i) :
          (less(a[k], a[j]) ? j : less(a[k], a[i]) ? k : i));
  }
  /* *********************************************************************
   *  Helper sorting functions
   ***********************************************************************/
  // is v < w ?
  private static <T extends Comparable<? super T>> boolean less(T v, T w) {
    if (COUNT_OPS) DoublingTest.incOps ();
    return (v.compareTo(w) < 0);
  }
  // does v == w ?
  private static <T extends Comparable<? super T>> boolean eq(T v, T w) {
    if (COUNT_OPS) DoublingTest.incOps ();
    return (v.compareTo(w) == 0);
  }
  // exchange a[i] and a[j]
  private static void exch(Object[] a, int i, int j) {
    Object swap = a[i];
    a[i] = a[j];
    a[j] = swap;
  }
  /* *********************************************************************
   *  Check if array is sorted - useful for debugging
   ***********************************************************************/
  private static <T extends Comparable<? super T>> boolean isSorted(T[] a) {
    for (int i = 1; i < a.length; i++)
      if (less(a[i], a[i-1])) return false;
    return true;
  }
  // test code
  private static boolean COUNT_OPS = false;
  public static void main(String[] args) {
    StdIn.fromFile ("data/words3.txt");
    String[] a = StdIn.readAllStrings();
    sort(a);
    // display results
    for (int i = 0; i < a.length; i++) {
      StdOut.println(a[i]);
    }
    StdOut.println("isSorted = " + isSorted(a));
    COUNT_OPS = true;
    DoublingTest.run (20000, 5, N -> ArrayGenerator.integerRandomUnique (N),          (Integer[] x) -> sort (x));
    DoublingTest.run (20000, 5, N -> ArrayGenerator.integerRandom (N, 2),             (Integer[] x) -> sort (x));
    DoublingTest.run (20000, 5, N -> ArrayGenerator.integerPartiallySortedUnique (N), (Integer[] x) -> sort (x));
  }
}
 |