数组中的逆序对

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;

}

 

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值