It is sometimes more efficient to implement Merge Sort in-place. Here is an implementation that sorts an two-dimensional array of strings according to a column, the explanations are made as comments:
publicstaticvoidmerge(String[][]in,String[][]out,intstart,intinc,intcol){intend1=Math.min(inc+start,in.length);intend2=Math.min(start+inc*2,in.length);intx=start;inty=start+inc;intz=start;while(x<end1&&y<end2){if(in[x][col].compareTo(in[y][col])<=0)/* the <= here is crucial since it allows for stable sorting */out[z++]=in[x++];elseout[z++]=in[y++];}if(x<end1)/* If right side of the array is consumed first, fill the rest up with the left side */for(inti=0;i<end1-x;i++)out[z+i]=in[x+i];elseif(y<end2)/* Do the opposite otherwise */for(inti=0;i<end2-y;i++)out[z+i]=in[y+i];}publicstaticvoidstableSort(String[][]intable,intcolumn){ints=1;String[][]table=intable;if(table==null||table.length<=1||0>column||column>=table[0].length)return;String[][]table2=newString[table.length][table[0].length];for(inti=0;i<table.length;i++)for(intj=0;j<table[i].length;j++)table2[i][j]=table[i][j];String[][]temp;intn=table.length;for(inti=1;i<n;i*=2){for(intj=0;j<n;j+=2*i)merge(table,table2,j,i,column);temp=table;table=table2;table2=temp;}if(intable!=table)for(inti=0;i<table.length;i++)intable[i]=table[i];}}