Mergesort

Question
Write an implementation of Mergesort, in Java, to sort a primitive array of integers.
 * 1) Is mergesort stable?
 * 2) What is the time complexity?
 * 3) Is this implementation in-place?

Answer
(written on paper and retyped here, hasn't been checked with a compiler yet)

Other

 * Yes, mergesort is a stable sort.
 * 1) The time complexity of mergesort is $$O(nlogn)$$, where n is the number of items to be sorted. This value is calculated by solving the recurrence relation: $$T(n) = 2T(n) + n$$ where T(n) is the amount of time required to merge sort a list of size n.
 * No.

Compiled
Here's a complete Java program that has a Merge class which implements merge sort on a primitive array of a generic type T which implements the Comparable interface.

This improves upon the implementation given above in two ways:
 * Can sort any type that implements Comparable, not just ints
 * Space complexity in this version is  as only a single extra array is used (declared in the public driver). The version above has   different arrays existing simultaneously, as the array is unfortunately declared locally in the recursive method.

The output of the program is the original array followed by the sorted array. {z, x, a, a, b, w, s, r, i, k, j, e, f, g, s, s, t, a, b, d} {a, a, a, b, b, d, e, f, g, i, j, k, r, s, s, s, t, w, x, z}

Sketch of time complexity proof

 * 1) Begin with the recurrence relation $$T(N) = 2T(N/2) + N$$ (where T(1) = 1), because mergesort cuts the amount of work in half at each recursive call, followed by linear time to merge the sorted sublists.
 * 2) Divide the recurrence relation by N
 * 3) Assume N = 2^k for some integer k. That is, we only allow N to be a power of 2.
 * 4) Write out the sequence of recurrence relations for all N = 2^k
 * 5) Add up the sequence of equations. A lot of cancellation occurs. Result will be $$T(N)/N = T(1)/1 + log(N)$$ because there are log(N) equations.
 * 6) Multiply through by N and O(Nlog(N)) run time is apparent.