public static void sort(double[] a, int left, int right, double[] tmp) { double[] left, right; if ( (rightleft) == 1 ) { // One element in array  No need to sort ! return; } int middle = (right + left)/2; sort( a, left, middle, tmp ); // Sort left HALF of a[ ] sort( a, middle, righ, tmp ); // Sort right HALF of a[ ] /* ========================================================== Merge both sorted arrays back ========================================================== */ merge( a, left, middle, right, tmp ); // Takes N steps to merge // an array of N elements // (It really takes 2*N steps, // but if you only want to // find the ORDER, you can use N) } 
T(N) = # statements that you need to execute to merge sort an array of N elements 
sort( array of N elements ) will call: sort( array of N/2 elements ); // Sort left HALF of array sort( array of N/2 elements ); // Sort right HALF of array merge( array of N elements ); // Takes N statements 
Therefore:
Amount of work done by sort(array of size N) = Amount of work done by sort(array of size N) + Amount of work done by sort(array of size N) + N 
Furthermore:
T(1) = 1 // Time to execute the ifstatement 
T(N) = 2*T(N/2) + N ...... (1) T(1) = 1 ...... (2) 
T(N) = 2*T(N/2) + N ( T(N/2) = 2*T(N/4) + N/2 ) = 2*(2*T(N/4) + N/2) + N = 4*T(N/4) + N + N = 4*T(N/4) + 2*N T(N) = 4*T(N/4) + 2*N ( T(N/4) = 2*T(N/8) + N/4 ) = 4*(2*T(N/8) + N/4) + 2*N = 8*T(N/8) + N + 2*N = 8*T(N/8) + 3*N T(N) = 8*T(N/8) + 3*N ( T(N/8) = 2*T(N/16) + N/8 ) = 8*(2*T(N/16) + N/8) + 3*N = 16*T(N/16) + N + 3*N = 16*T(N/16) + 4*N And so on.... 
We can detect this pattern:
T(N) = 2*T(N/2) + N = 4*T(N/4) + 2*N = 2^{2}*T(N/(2^{2})) + 2*N = 8*T(N/8) + 3*N = 2^{3}*T(N/(2^{3})) + 3*N 
For example:
N = 1024 (2^{10}) or: N = 1048576 (2^{20}) 
If N is equal to some power of 2, we can solve T(N) exactly !
For example:
If N = 1024 (2^{10}) T(N) = 2*T(N/2) + N = 4*T(N/4) + 2*N = 8*T(N/8) + 3*N = 16*T(N/16) + 4*N = 32*T(N/32) + 5*N = ... = 1024*T(N/1024) + 10*N (N = 1024) = N*T(1) + 10*N (T(1) = 1 !) = N + 10*N = 11*N 
If N = 2^{k} T(N) = 2*T(N/2) + N = 4*T(N/4) + 2*N = 8*T(N/8) + 3*N = 16*T(N/16) + 4*N = 32*T(N/32) + 5*N = ... = (2^{k})*T(N/(2^{k})) + k*N (N = 2^{k}, so: N/(2^{k}) = 1) = (2^{k})*T(1) + k*N (2^{k}= N) = N*T(1) + k*N (T(1) = 1) = N + k*N = (k+1)*N (N = 2^{k} ⇒ k = lg(N)) = (lg(N)+1)*N = (log(N)+1)*lg(N) 


How to run the program:

How to run the program:
