我的mergeSort的简单实现
public static long mergeSort(int[] Demo,int S,int e){
long count=0l;
if (S<e){
int mid = S + ((e - S) >> 1);
count+=mergeSort(Demo, S, mid);
count+=mergeSort(Demo, mid + 1, e);
count+=merge(Demo, S, mid, e);
}
return count;
}
public static long merge(int[]nums,int s,int mid,int e){
long count=0l;
int[]temp=new int[e-s+1];
int d = s;
int secondStart = mid + 1;
int start = 0;
while (d <= mid && secondStart <= e) {
if (nums[d] <= nums[secondStart]) {
temp[start] = nums[d];
d++;
start++;
} else {
count = count + mid - d + 1;
temp[start] = nums[secondStart];
secondStart++;
start++;
}
}
while (d <= mid) {
temp[start] = nums[d];
start++;
d++;
}
while (secondStart <= e) {
temp[start] = nums[secondStart];
secondStart++;
start++;
}
for (int i = 0; i < temp.length; i++) {
int Demo2=i+s;
nums[Demo2] = temp[i];
}
return count;
}