/** * Perform a merge sort on a range of a int array using numerical order. * @param array the array. * @param start the first index. * @param end the last index (exclusive). * @param comp the comparator object. */ public static void mergeSort(int[] array, int start, int end, IntComparator comp) { checkBounds(array.length, start, end); int[] out = Arrays.copyOf(array, array.length); mergeSort(out, array, start, end, comp); }
mergeSort(out, in, start, med, c); mergeSort(out, in, med, end, c); T rVal = in[r]; if (c.compare(fromVal, rVal) <= 0) { int l_1 = find(in, rVal, -1, start + 1, med - 1, c); int toCopy = l_1 - start + 1; System.arraycopy(in, start, out, i, toCopy); start = l_1 + 1; } else { int r_1 = find(in, fromVal, 0, r + 1, end - 1, c); int toCopy = r_1 - r + 1; System.arraycopy(in, r, out, i, toCopy);
if (length > 40) { length /= 8; bottom = med3(array, bottom, bottom + length, bottom + (2 * length), comp); middle = med3(array, middle - length, middle, middle + length, comp); top = med3(array, top - (2 * length), top - length, top, comp); middle = med3(array, bottom, middle, top, comp); quickSort0(start, start + length, array, comp); quickSort0(end - length, end, array, comp);
/** * Sorts some external data with QuickSort. * * @param start * the start index to sort. * @param end * the last + 1 index to sort. * @param comp * the comparator. * @param swap an object that can exchange the positions of two items. * @throws IllegalArgumentException * if {@code start > end}. * @throws ArrayIndexOutOfBoundsException * if {@code start < 0} or {@code end > array.length}. */ public static void quickSort(int start, int end, IntComparator comp, Swapper swap) { checkBounds(end + 1, start, end); quickSort0(start, end, comp, swap); }
private static void quickSort0(int start, int end, IntComparator comp, Swapper swap) { int length = end - start; if (length < 7) { insertionSort(start, end, comp, swap); return; bottom = med3(bottom, bottom + skosh, bottom + (2 * skosh), comp); middle = med3(middle - skosh, middle, middle + skosh, comp); top = med3(top - (2 * skosh), top - skosh, top, comp); middle = med3(bottom, middle, top, comp); quickSort0(start, start + length, comp, swap); quickSort0(end - length, end, comp, swap);
/** * Sort the specified range of an array of object that implement the Comparable * interface. * @param <T> The type of object. * @param array the array. * @param start the first index. * @param end the last index (exclusive). */ public static <T extends Comparable<? super T>> void quickSort(T[] array, int start, int end) { quickSort(array, start, end, new ComparableAdaptor<T>()); }
/** * Perform a merge sort on a range of a byte array, using numerical order. * @param array the array. * @param start the first index. * @param end the last index (exclusive). */ public static void mergeSort(byte[] array, int start, int end) { mergeSort(array, start, end, NATURAL_BYTE_COMPARISON); }
if (middle - first > last - middle) { firstCut = first + (middle - first) / 2; secondCut = lowerBound(middle, last, firstCut, comp); } else { secondCut = middle + (last - middle) / 2; firstCut = upperBound(first, middle, secondCut, comp); inplaceMerge(first, firstCut, middle, comp, swapper); inplaceMerge(middle, secondCut, last, comp, swapper);
mergeSort(fromIndex, mid, c, swapper); mergeSort(mid, toIndex, c, swapper); inplaceMerge(fromIndex, mid, toIndex, c, swapper);
/** * Sorts the specified range in the array in a specified order. * * @param array * the {@code int} array to be sorted. * @param start * the start index to sort. * @param end * the last + 1 index to sort. * @param comp * the comparator. * @throws IllegalArgumentException * if {@code start > end}. * @throws ArrayIndexOutOfBoundsException * if {@code start < 0} or {@code end > array.length}. */ public static void quickSort(int[] array, int start, int end, IntComparator comp) { Preconditions.checkNotNull(array); checkBounds(array.length, start, end); quickSort0(start, end, array, comp); }
private static void quickSort0(int start, int end, IntComparator comp, Swapper swap) { int length = end - start; if (length < 7) { insertionSort(start, end, comp, swap); return; bottom = med3(bottom, bottom + skosh, bottom + (2 * skosh), comp); middle = med3(middle - skosh, middle, middle + skosh, comp); top = med3(top - (2 * skosh), top - skosh, top, comp); middle = med3(bottom, middle, top, comp); quickSort0(start, start + length, comp, swap); quickSort0(end - length, end, comp, swap);
Sorting.quickSort(0, keyList.size(), comp, swapper);
/** * Perform a merge sort on a range of a short array, using numerical order. * @param array the array. * @param start the first index. * @param end the last index (exclusive). */ public static void mergeSort(short[] array, int start, int end) { mergeSort(array, start, end, NATURAL_SHORT_COMPARISON); }
if (middle - first > last - middle) { firstCut = first + (middle - first) / 2; secondCut = lowerBound(middle, last, firstCut, comp); } else { secondCut = middle + (last - middle) / 2; firstCut = upperBound(first, middle, secondCut, comp); inplaceMerge(first, firstCut, middle, comp, swapper); inplaceMerge(middle, secondCut, last, comp, swapper);
mergeSort(fromIndex, mid, c, swapper); mergeSort(mid, toIndex, c, swapper); inplaceMerge(fromIndex, mid, toIndex, c, swapper);
mergeSort(out, in, start, med, c); mergeSort(out, in, med, end, c); long rVal = in[r]; if (c.compare(fromVal, rVal) <= 0) { int l_1 = find(in, rVal, -1, start + 1, med - 1, c); int toCopy = l_1 - start + 1; System.arraycopy(in, start, out, i, toCopy); start = l_1 + 1; } else { int r_1 = find(in, fromVal, 0, r + 1, end - 1, c); int toCopy = r_1 - r + 1; System.arraycopy(in, r, out, i, toCopy);
if (length > 40) { length /= 8; bottom = med3(array, bottom, bottom + length, bottom + (2 * length), comp); middle = med3(array, middle - length, middle, middle + length, comp); top = med3(array, top - (2 * length), top - length, top, comp); middle = med3(array, bottom, middle, top, comp); quickSort0(start, start + length, array, comp); quickSort0(end - length, end, array, comp);
/** * Perform a merge sort on a range of a long array using a specified ordering. * @param array the array. * @param start the first index. * @param end the last index (exclusive). * @param comp the comparator object. */ public static void mergeSort(long[] array, int start, int end, LongComparator comp) { checkBounds(array.length, start, end); long[] out = Arrays.copyOf(array, array.length); mergeSort(out, array, start, end, comp); }
/** * Sorts the specified range in the array in a specified order. * * @param array * the {@code float} array to be sorted. * @param start * the start index to sort. * @param end * the last + 1 index to sort. * @param comp * the comparator. * @throws IllegalArgumentException * if {@code start > end}. * @throws ArrayIndexOutOfBoundsException * if {@code start < 0} or {@code end > array.length}. */ public static void quickSort(float[] array, int start, int end, FloatComparator comp) { Preconditions.checkNotNull(array); checkBounds(array.length, start, end); quickSort0(start, end, array, comp); }
private static void quickSort0(int start, int end, IntComparator comp, Swapper swap) { int length = end - start; if (length < 7) { insertionSort(start, end, comp, swap); return; bottom = med3(bottom, bottom + skosh, bottom + (2 * skosh), comp); middle = med3(middle - skosh, middle, middle + skosh, comp); top = med3(top - (2 * skosh), top - skosh, top, comp); middle = med3(bottom, middle, top, comp); quickSort0(start, start + length, comp, swap); quickSort0(end - length, end, comp, swap);