@@ -0,0 +1,1032 @@
+package sorting;%0A%0Aimport java.util.Comparator;%0A%0A/**%0A * Merge two sorted arrays A of length M and B of length N into the same array%0A * A. Assume that A has enough space in the end to accomodate all elements of B.%0A * i.e. A's size is M + N.%0A */%0Apublic class MergeWithSpaceForOtherArray %7B%0A%09public static %3CT%3E void merge(T%5B%5D bigArray, int bigArraySize,%0A%09%09%09T%5B%5D smallArray, Comparator%3CT%3E comparator) %7B%0A%09%09if (bigArray == null %7C%7C smallArray == null) %7B%0A%09%09%09throw new NullPointerException(%22You are a stupid programmer.%22);%0A%09%09%7D%0A%0A%09%09int bigArrayIndex = bigArraySize - 1;%0A%09%09int smallArrayIndex = smallArray.length - 1;%0A%09%09int sortedIndex = bigArray.length - 1;%0A%09%09while (smallArrayIndex %3E= 0 && bigArrayIndex %3E= 0) %7B%0A%09%09%09if (comparator.compare(smallArray%5BsmallArrayIndex%5D,%0A%09%09%09%09%09bigArray%5BbigArrayIndex%5D) %3C 0) %7B%0A%09%09%09%09bigArray%5BsortedIndex--%5D = bigArray%5BbigArrayIndex--%5D;%0A%09%09%09%7D else %7B%0A%09%09%09%09bigArray%5BsortedIndex--%5D = smallArray%5BsmallArrayIndex--%5D;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09while (smallArrayIndex %3E= 0) %7B%0A%09%09%09bigArray%5BsortedIndex--%5D = smallArray%5BsmallArrayIndex--%5D;%0A%09%09%7D%0A%09%7D%0A%7D%0A

Merge two sorted arrays with one containing space for the other.