/** Sorts an array according to the order induced by the specified * comparator using quicksort. * * <p>The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas * McIlroy, “Engineering a Sort Function”, <i>Software: Practice and Experience</i>, 23(11), pages * 1249−1265, 1993. * * @param x the array to be sorted. * @param comp the comparator to determine the sorting order. * */ public static void quickSort( final boolean[] x, final BooleanComparator comp ) { quickSort( x, 0, x.length, comp ); } @SuppressWarnings("unchecked")
/** Sorts an array according to the natural ascending order using quicksort. * * <p>The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas * McIlroy, “Engineering a Sort Function”, <i>Software: Practice and Experience</i>, 23(11), pages * 1249−1265, 1993. * * @param x the array to be sorted. * */ public static void quickSort( final boolean[] x ) { quickSort( x, 0, x.length ); } /** Sorts the specified range of elements according to the natural ascending order using mergesort, using a given support array.
/** * Sorts an array according to the natural ascending order using quicksort. * * <p> * The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. * Douglas McIlroy, “Engineering a Sort Function”, <i>Software: * Practice and Experience</i>, 23(11), pages 1249−1265, 1993. * * <p> * Note that this implementation does not allocate any object, contrarily to the * implementation used to sort primitive types in {@link java.util.Arrays}, * which switches to mergesort on large inputs. * * @param x * the array to be sorted. * */ public static void quickSort(final boolean[] x) { quickSort(x, 0, x.length); } protected static class ForkJoinQuickSort extends RecursiveAction {
/** * Sorts an array according to the order induced by the specified comparator * using quicksort. * * <p> * The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. * Douglas McIlroy, “Engineering a Sort Function”, <i>Software: * Practice and Experience</i>, 23(11), pages 1249−1265, 1993. * * <p> * Note that this implementation does not allocate any object, contrarily to the * implementation used to sort primitive types in {@link java.util.Arrays}, * which switches to mergesort on large inputs. * * @param x * the array to be sorted. * @param comp * the comparator to determine the sorting order. * */ public static void quickSort(final boolean[] x, final BooleanComparator comp) { quickSort(x, 0, x.length, comp); } protected static class ForkJoinQuickSortComp extends RecursiveAction {
/** * Sorts two arrays according to the natural lexicographical ascending order * using quicksort. * * <p> * The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. * Douglas McIlroy, “Engineering a Sort Function”, <i>Software: * Practice and Experience</i>, 23(11), pages 1249−1265, 1993. * * <p> * This method implements a <em>lexicographical</em> sorting of the arguments. * Pairs of elements in the same position in the two provided arrays will be * considered a single key, and permuted accordingly. In the end, either * {@code x[i] < x[i + 1]} or <code>x[i] * == x[i + 1]</code> and {@code y[i] ≤ y[i + 1]}. * * @param x * the first array to be sorted. * @param y * the second array to be sorted. */ public static void quickSort(final boolean[] x, final boolean[] y) { ensureSameLength(x, y); quickSort(x, y, 0, x.length); } protected static class ForkJoinQuickSort2 extends RecursiveAction {
@Override public void sort() { BooleanArrays.quickSort(elements(), 0, size()); }
/** * Sorts the specified range of elements according to the natural ascending * order using a parallel quicksort. * * <p> * The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. * Douglas McIlroy, “Engineering a Sort Function”, <i>Software: * Practice and Experience</i>, 23(11), pages 1249−1265, 1993. * * <p> * This implementation uses a {@link ForkJoinPool} executor service with * {@link Runtime#availableProcessors()} parallel threads. * * @param x * the array to be sorted. * @param from * the index of the first element (inclusive) to be sorted. * @param to * the index of the last element (exclusive) to be sorted. */ public static void parallelQuickSort(final boolean[] x, final int from, final int to) { if (to - from < PARALLEL_QUICKSORT_NO_FORK) quickSort(x, from, to); else { final ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); pool.invoke(new ForkJoinQuickSort(x, from, to)); pool.shutdown(); } } /**
final BooleanComparator comp) { if (to - from < PARALLEL_QUICKSORT_NO_FORK) quickSort(x, from, to, comp); else { final ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
quickSort(x, y, from, to); final ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); pool.invoke(new ForkJoinQuickSort2(x, y, from, to));
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, y, from, to); return;
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, from, to); return;
final int len = to - from; if (len < PARALLEL_QUICKSORT_NO_FORK) { quickSort(x, from, to, comp); return;