自底向上的归并排序
public static int mergeSort2(int[] arr){
int end = arr.length-1;
int count=0;
int step = 1;
while (step<arr.length) {
for(int i=0;i<arr.length-step;i+=2*step) {
merge(arr,i,i+step-1,Math.min(i+2*step-1,end));
count++;
}
step*=2;
}
return count;
}
public static int mergeSort(int[] arr) {
int count=0;
int dx = 1;
while(dx<arr.length) {
int m = 0,l=0,r=0;
while (l+dx*2-1<arr.length)
{
count++;
m = l+dx-1;
r = l+ dx*2-1;
merge(arr,l,m,r);
l = r+1;
}
m = l+dx-1;
if(m<arr.length-1)
{
count++;
merge(arr,l,m,arr.length-1);
}
dx*=2;
}
return count;
}