</pre><pre code_snippet_id="1593425" snippet_file_name="blog_20160301_3_5306061" name="code" class="java">public class MergeSort {
public int B[];
public int[] mergeSort(int[] A, int n) {
// write code here
B = new int[n];
myMergeSort(A, 0, n-1);
return A;
}
//递归分治
public void myMergeSort(int[] A, int low, int hight){
if(low < hight){
int mid = (low+hight)/2;
myMergeSort(A,low,mid);
myMergeSort(A,mid+1,hight);
merge(A,low,mid,hight);
}
}
//合并
public void merge(int[]A,int low, int mid, int hight){
int i,j,k;
for(i=low ;i<=hight;i++){
B[i] = A[i];
}
for(i=low,j=mid+1,k=i; i<=mid&&j<=hight; k++){
if(B[i]<=B[j]){
A[k] = B[i++];
}else{
A[k] = B[j++];
}
}
while(i<=mid) A[k++] = B[i++];
while(j<=hight) A[k++] = B[j++];
}
}