开发者

Using Mergesort to calculate number of inversions in C++

开发者 https://www.devze.com 2022-12-24 09:36 出处:网络
void MergeSort(int A[], int n, int B[], int C[]) { if(n > 1) { Copy(A,0,floor(n/2),B,0,floor(n/2)); Copy(A,floor(n/2),n-1,C,0,floor(n/2)-1);
void MergeSort(int A[], int n, int B[], int C[])
{ 
    if(n > 1)
    {
       Copy(A,0,floor(n/2),B,0,floor(n/2));
       Copy(A,floor(n/2),n-1,C,0,floor(n/2)-1);
       MergeSort(B,floor(n/2),B,C);
       MergeSort(C,floor(n/2),B,C);
       Merge(A,B,0,floor(n/2),C,0,floor(n/2)-1);
    }
};

void Copy(int A[], int startIndexA, int endIndexA, int B[], int startIndexB, int endIndexB)
{
    while(startIndexA < endIndexA && startIndexB < endIndexB)
    {
        B[startIndexB]=A[startIndexA];
        startIndexA++;
        startIndexB++;
    }
 };

 void Merge(int A[], int B[],int leftp, int rightp, int C[], int leftq, int rightq) 
//H开发者_开发问答ere each sub array (B and C) have both left and right indices variables (B is an array with p elements and C is an element with q elements)
{ 
    int i=0;
    int j=0;
    int k=0;

    while(i < rightp && j < rightq)
    {
        if(B[i] <=C[j])
        {
            A[k]=B[i];
            i++;
        }
       else
       {
            A[k]=C[j];
            j++;
            inversions+=(rightp-leftp); //when placing an element from the right array, the number of inversions is the number of elements still in the left sub array.
        }
        k++;
  }
  if(i=rightp)
      Copy(A,k,rightp+rightq,C,j,rightq);
  else
      Copy(A,k,rightp+rightq,B,i,rightp);
}

I am specifically confused on the effect of the second 'B' and 'C' arguments in the MergeSort calls. I need them in there so I have access to them for Copy and and Merge, but


I apologize for the ambiguity here. Here is the output:

Input (A)=  4   2   53  8   1   19  21   6 
 19 
 19 
 21  
 19 
 21 
 19 
 21 
 6 
 inversions=9

Obviously that is not the correct result for the array, and by my count, the inversions should equal 16. Any help or comments would be greatly appreciated. (even criticism too! ;)


Psuedo-code from Intro to Algorithms (Cormen. MIT Press) 2nd Edition:

MERGE -INVERSIONS ( A, p, q, r)
n1 ← q − p + 1
n2 ← r − q
create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
for i ← 1 to n1
  do L[i] ← A[ p + i − 1]
for j ← 1 to n2
  do R[ j ] ← A[q + j ]
L[n 1 + 1] ← ∞
R[n 2 + 1] ← ∞
i ←1
j ←1
inversions ← 0
counted ← FALSE
for k ← p to r
  do 
  if counted = FALSE and R[ j ] < L[i]
    then inversions ← inversions +n1 − i + 1
    counted ← TRUE
  if L[i] ≤ R[ j ]
    then A[k] ← L[i]
    i ←i +1
  else A[k] ← R[ j ]
    j ← j +1
    counted ← FALSE
  return inversions

One should note that the counted variable is actually not needed. Merge sort is an inherently recursive algorithm by nature. Your implementation should closely follow this psuedo-code. While adapting where necessary to suit your needs. However in this case. A direct implementation of this psuedo-code will in fact count inversions during a classic merge sort.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号