Merge Sort: Counting Inversions

  • + 2 comments

    I have two questions related to your answer.

    How did you find the logic to calcuate the value of count? Is there someplace I can read about it more?

    count += mid + 1 - i;
    

    Second. I made a mistake of swapping aux and arr in the following code based on the function definition and I was getting my answer wrong. Why are you switching? What is the reasoning behind it?

    count += countInversions(aux, lo, mid, arr);
    count += countInversions(aux, mid + 1, hi, arr);