相关代码
class Sort5{
public void mergeSort(int number[]){
if (number.length <= 1 || number == null)
return;
else {
merge(number,0,number.length);
}
}
public void merge(int number[],int left,int right){
if(left>=right-1)
return;
int mid=(left+right)/2;
merge(number,left,mid);
merge(number,mid,right);
sort(number,left,mid,right);
}
public void sort(int number[],int left,int mid,int right){
int i=left;
int j=mid;
int k=0;
int length=right-left;
int extra[]=new int[length];
while (i<mid&&j<right){
if(number[i]<=number[j])
extra[k++]=number[i++];
else
extra[k++]=number[j++];
}
while(i<mid){
extra[k++]=number[i++];
}
while (j<right){
extra[k++]=number[j++];
}
int a=0;
while (a<length){
number[a+left]=extra[a];
}
}
}