public class MergeArray
{
    public static void printArray(int[] a) {
	for (int x : a)
	    System.out.print(x + "  ");
	System.out.println();
    }

    /** Given two non-null sorted arrays a and b, return in a new array
	their merge
    */
    public static int[] merge(int[] a, int[] b) {
	int[] c = new int[a.length+b.length];
	int aCursor = 0, bCursor = 0, cCursor = 0;
	while (aCursor < a.length && bCursor < b.length) {
	    if (a[aCursor] <= b[bCursor])
		c[cCursor++] = a[aCursor++];
	    else
		c[cCursor++] = b[bCursor++];
	}
	while (aCursor < a.length) {
	    c[cCursor++] = a[aCursor++];
	}
	while (bCursor < b.length) {
	    c[cCursor++] = b[bCursor++];
	}
	return c;
    }

    public static void main(String[] args) {
	int[] joe = {1,3,5,7};
	int[] jim = {2,8,10,11,14};
	int[] jack = merge(joe, jim);
	System.out.println("The merge of");
	printArray(joe);
	System.out.println("and");
	printArray(jim);
	System.out.println("is");
	printArray(jack);
    }
}
