PrimitiveArraySorter { public static boolean sortAndMerge(short[] array, short[] mergeArray, int pivotIndex, int toIndex, ShortComparator comparator) { if (array.length == 1) return false; sort(array, 0, pivotIndex, comparator); if (pivotIndex == toIndex || comparator.compare(array[pivotIndex - 1], array[pivotIndex]) <= 0) { return false; } merge(array, mergeArray, pivotIndex, toIndex, comparator); return true; } private  PrimitiveArraySorter(); static void sort(short[] array, ShortComparator comparator); static void sort(short[] array, int fromIndex, int toIndex, ShortComparator comparator); static boolean sortAndMerge(short[] array, short[] mergeArray, int pivotIndex, int toIndex, ShortComparator comparator); static void merge(short[] array, short[] mergeArray, int pivotIndex, ShortComparator comparator); static void merge(short[] array, short[] mergeArray, int pivotIndex, int toIndex, ShortComparator comparator);  }
@Test public void test_sortandmerge_returns_false_when_sort_is_in_place() { short[] array = {3, 2, 1, 0, 4, 5, 6}; short[] mergeArray = new short[array.length]; assertFalse(PrimitiveArraySorter.sortAndMerge(array, mergeArray, 4, 7, Short::compare)); assertIsSorted(array); array = new short[]{3, 2, 1, 0, 4, 5, 6}; assertTrue(PrimitiveArraySorter.sortAndMerge(array, mergeArray, 3, 7, Short::compare)); assertIsSorted(mergeArray); }