int g_sum;
void SortDate(int *nums, int *store, int start, int mid, int end)
{
int point1 = start;
int point2 = mid + 1;
int storepoint = 0;
if (nums == NULL || store == NULL) {
return;
}
if (start >= end) {
return;
}
while (point1 <= mid && point2 <= end) {
if (nums[point1] <= nums[point2]) {
store[storepoint++] = nums[point1];
g_sum += (point2 - mid - 1);
point1++;
} else {
store[storepoint++] = nums[point2];
point2++;
}
}
while (point1 <= mid) {
store[storepoint++] = nums[point1];
g_sum += (point2 - mid - 1);
point1++;
}
while (point2 <= end) {
store[storepoint++] = nums[point2];
point2++;
}
int j = 0;
for (int i = start; i <= end; i++, j++) {
nums[i] = store[j];
}
}
void TwoFen(int *nums, int *store, int start, int end)
{
if (nums == NULL) {
return;
}
if (start >= end) {
return;
}
int mid = (start + end) / 2; //没有对整数溢出进行处理
TwoFen(nums, store, start, mid);
TwoFen(nums, store, mid + 1, end);
SortDate(nums, store, start, mid, end);
}
int reversePairs(int* nums, int numsSize){
int start = 0;
int end = numsSize - 1;
g_sum = 0;
int *store = (int *)malloc(sizeof(int) * numsSize);
TwoFen(nums, store, start, end);
return g_sum;
}