public class NormalMergeSort {
int[] arr;
public NormalMergeSort(int[] arr){
this.arr = arr;
}
public void MergeSort(){
MergeSort(0,this.arr.length-1);
}
public void MergeSort(int left,int right){
if(left<right){
MergeSort(left,(left+right)/2);
MergeSort((left+right)/2+1,right);
Merge(left,(left+right)/2,(left+right)/2+1,right);
}
}
public void Merge(int left,int leftEnd,int right,int rightEnd){
final int originIndex = left; //保留开头下标
int[] tmpArr = new int[rightEnd-left+1];
int tmpArrIndex = 0;
while(left<=leftEnd&&right<=rightEnd){
if(this.arr[left]<=this.arr[right]){
tmpArr[tmpArrIndex++] = this.arr[left++];
}else{
tmpArr[tmpArrIndex++] = this.arr[right++];
}
}
while(left<=leftEnd){
tmpArr[tmpArrIndex++]=this.arr[left++];
}
while (right <= rightEnd) {
tmpArr[tmpArrIndex++] = this.arr[right++];
}
tmpArrIndex = 0;
for(int Index = originIndex;Index<=rightEnd;Index++,tmpArrIndex++){
this.arr[Index] = tmpArr[tmpArrIndex];
}
}
public static void main(String[] args) {
NormalMergeSort instance = new NormalMergeSort(new int[]{4,2,6,2,5,3,6,8,3,4,7});
instance.MergeSort();
int[] result = instance.arr; //断点测试
}
}