计算c+中mergesort算法中的掉期数量+; 我在GeksFureGeK.Org中有C++的合并算法,我试图在数组最后排序之前计算数组上的交换数。我已经有了mesgesorting的想法,但是我发现mergesort算法有点令人沮丧

计算c+中mergesort算法中的掉期数量+; 我在GeksFureGeK.Org中有C++的合并算法,我试图在数组最后排序之前计算数组上的交换数。我已经有了mesgesorting的想法,但是我发现mergesort算法有点令人沮丧,c++,algorithm,mergesort,C++,Algorithm,Mergesort,代码如下: // Merge Sort #include <iostream> using namespace std; // This function Merges two subarrays // First subarray is stored in arr[l..m] // Second subarray is stored in arr[m+1..r] int count = 0; void merge(int arr[], int l, int m, int r)

代码如下:

// Merge Sort
#include <iostream>
using namespace std;

// This function Merges two subarrays
// First subarray is stored in arr[l..m]
// Second subarray is stored in arr[m+1..r]
int count = 0;

void merge(int arr[], int l, int m, int r) {
    int n1 = m - l + 1;
    int n2 = r - m;

    // Create temp arrays
    int L[n1], R[n2];

    // Copy data to temp arrays L[] and R[]
    for (int i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (int j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    // indexes initialization
    int i = 0;
    int j = 0;
    int k = l;

    // merges the subarrays
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
            count++;
        } else {
            arr[k] = R[j];
            j++;
            count++;
        }
        k++;
    }

    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
        //count++;
    }

    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
        //count++;
    }
}
 
void mergeSort(int arr[], int l, int r) {
    if (l >= r) {
        return;
    }
    int m = (l + r - 1) / 2;
    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);
    merge(arr, l, m, r);
}

// main code

int main() {
    int arr[] = { 7, 4, 45, 67, 83, 90, 18, 2, 11, 8, 6, 18, 9, 15, 23, 5 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);

    // print given aray
    cout << "Given array is \n";
    for (int i = 0; i < arr_size; i++)
        cout << arr[i] << " ";

    mergeSort(arr, 0, arr_size - 1);

    // print sorted array
    cout << "\nSorted array is \n";
    for (int i = 0; i < arr_size; i++)
        cout << arr[i] << " ";

    cout << "\nThe numbers of swaps for the sorted array are " << count;
}
//合并排序
#包括
使用名称空间std;
//此函数合并两个子阵列
//第一个子阵列存储在arr[l..m]中
//第二个子阵列存储在arr[m+1..r]中
整数计数=0;
无效合并(整数arr[],整数l,整数m,整数r){
int n1=m-l+1;
int n2=r-m;
//创建临时阵列
int L[n1],R[n2];
//将数据复制到临时数组L[]和R[]
对于(int i=0;i我刚刚找到了解决办法:

您需要补充的是:

 void merge(int arr[], int l, int m, int r)
{

 int n1 = m - l + 1;
 int n2 = r - m;

 // Create temp arrays
 int L[n1], R[n2];

// Copy data to temp arrays L[] and R[]
for (int i = 0; i < n1; i++)
    L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
    R[j] = arr[m + 1 + j];



// indexes initialization
int i = 0;
int j = 0;
int k = l;

// merges the subarrays
while (i < n1 && j < n2) {
    if (L[i] <= R[j]) {
        arr[k] = L[i];
        i++;

    }
    else {
        arr[k] = R[j];
        j++;
        count+=n1-i;
    }
    k++;

}
void合并(int arr[],int l,int m,int r)
{
int n1=m-l+1;
int n2=r-m;
//创建临时阵列
int L[n1],R[n2];
//将数据复制到临时数组L[]和R[]
对于(int i=0;iif(L[i]合并排序非常简单,可以归结为递归交换。我认为您应该首先了解它在纸面上是如何工作的,然后使用断点调试您的代码。我在youtue上看到了一个视频,使其更加清晰。但仍然找不到解决计数器问题的方法。@MichałKaczorowskiTry调试您的代码。设置一些断点ts,模拟非常简单和可预测的输入数组,通过排序和合并一步一步地进行排序。你使用什么IDE?我使用的是代码块。我的老师给了我上面的代码,并告诉我唯一可以做的更改是添加我自己的代码,而不是更改初始值。顺便说一句,谢谢你的指示。。。