static void Merge(int[] A, int p, int q, int r) {
    	
    	//Allocate dynamic memory for L and R within merge.
    	int ls = q-p+1;
    	int rs = r-q;
        int[] L = new int[ls];
        int[] R = new int[rs];
        
        //Copy A[p...q] into L[0...ls-1]; A[q+1,r] into R[0...rs-1]        
        for(int i=p; i<=q; i++){ 
        	L[i-p] = A[i];
        }
        
        for(int i=q+1; i<=r; i++){ 
        	R[i-(q+1)] = A[i];
        }
        
        //Merge the data in L and R back to A
        int i = 0; int j = 0;
        
        for(int k=p; k<=r; k++) {
        	if ((j>=rs) || ((i<ls) && (L[i] <= R[j])))
        		A[k] = L[i++];
        	else
        		A[k] = R[j++];
        }
        return;	
    }