归并排序C++实现及求逆序对的个数

1.归并排序的递归实现:

#include<iostream>
#include<vector>
#include<string>
#include<strstream>
#include<algorithm>
#include<unordered_map>
using namespace std;
void MergeSort(vector<int>& nums, vector<int> &copy, int start, int end)
{
    if (start == end)
    {
        copy[start] = nums[start];
        return;
    }
    int len = (end - start) / 2;
    MergeSort(nums, copy, start, start + len);
    MergeSort(nums, copy, start + len + 1, end);
    int i = start, j = start + len + 1, index = start;
    while (i <= start + len && j <= end)
    {
        if (nums[i] < nums[j])
        {
            copy[index] = nums[i];
            i++;
        }
        else
        {
            copy[index] = copy[j];
            j++;
        }
        index++;
    }
    while (i <= start + len)//如果前半段有剩余则复制到copy数组中
    {
        copy[index] = nums[i];
        ++i;
        ++index;
    }
    while (j <= end)//如果后半段有剩余则复制到copy数组中
    {
        copy[index] = nums[j];
        ++j;
        ++index;
    }
    for (int i = start;i <= end;i++)//此时copy数组已经有序,将其复制到原数组中
        nums[i] = copy[i];
}
int main()
{
    int a[8] = { 8, 4 ,7,0,3,5,9,1 };
    vector<int> nums(a, a + 8);
    int len = nums.size();
    vector<int> copy(len);
    MergeSort(nums, copy, 0, len - 1);
    for (auto e : nums)
        cout << e << " ";
    cout << endl;
    for (auto e : copy)
        cout << e << " ";
    cout << endl;
    return 0;
}

2. 求逆序对的个数

只需将归并排序的算法稍微修改一下即可

#include<iostream>
#include<vector>
#include<string>
#include<strstream>
#include<algorithm>
#include<unordered_map>
using namespace std;
int MergeSort(vector<int>& nums, vector<int> &copy, int start, int end)
{
    if (start == end)
    {
        copy[start] = nums[start];
        return 0;
    }
    int len = (end - start) / 2;
    int left = MergeSort(nums, copy, start, start + len);
    int right = MergeSort(nums, copy, start + len + 1, end);
    int i = start, j = start + len + 1, index = start;
    int cnt = 0;
    while (i <= start + len && j <= end)
    {
        if (nums[i] < nums[j])
        {
            copy[index] = nums[i];
            i++;
        }
        else
        {
            copy[index] = copy[j];
            j++;
            cnt += start + len - i + 1;//次数统计逆序对
        }
        index++;
    }
    while (i <= start + len)//如果前半段有剩余则复制到copy数组中
    {
        copy[index] = nums[i];
        ++i;
        ++index;
    }
    while (j <= end)//如果后半段有剩余则复制到copy数组中
    {
        copy[index] = nums[j];
        ++j;
        ++index;
    }
    for (int i = start;i <= end;i++)//此时copy数组已经有序,将其复制到原数组中
        nums[i] = copy[i];
    return left + right + cnt;
}
int main()
{
    int a[8] = { 7,5,6,4 };
    vector<int> nums(a, a + 4);
    int len = nums.size();
    vector<int> copy(len);
    cout << "kkk "<<MergeSort(nums, copy, 0, len - 1) << endl;
    for (auto e : nums)
        cout << e << " ";
    cout << endl;
    for (auto e : copy)
        cout << e << " ";
    cout << endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/vincent93/p/7624304.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值