I've implemented the merge sort algorithm and used the 'merge' part for counting the number of split-inversions in an array as part of an assignment for an online course. How ever, the out put array is not a sorted version of the input array and as a result the number of split inversions obtained is wrong. I think that there is some thing wrong in the way I am indexing arrays.
P.s: I've used ' cout ' to print the values of indexes to see exactly what values are being passed in during the recursions.
[Code]
#include <iostream>
using namespace std;
int length=0,mid=0,inv=0;
void mergesort(int arr[], int first, int last)
{
cout << "first: " << first << " " << "last: " << last;
cout << endl;
void merge_and_countinv(int arr[], int first, int mid, int last);
Oh I am so sorry. After posting I felt that my post was inappropriate for the Beginner forum hence I' decided to post again on the General forum. I am new to using this web-site's forum. Again, apologies for double posting. I have deleted the post from the Beginner's forum.
First of all you have a serious problem with global variables.
Global variable mid will be rewritten by first recursive call of mergesort and second call will take invalid midpoint. This is the cause of your 6 duplication.
You can leave inv global (as it is debug counter which does not participate in logic) but other variables should be local.
I do not have time to delve into merging code right now, but I will check it later.
Thank you so much MiiNiPaa for pointing me to the problem with the global variables. After declaring them local in the merge-sort routine I got better results, specifically an output array without the duplicate 6 like you mentioned.
After a little more inspection I found that I was not using correct values for the index in the loop used to copy values from the 'temp' array into the input array. After using correct values, the program runs as expected and yields a sorted array.
Now I just have to fix the part of the program used to count the number of split-inversions.
I was very frustrated since quite a few days as I just couldn't find the problem. Thank you once again for your time and pointing to the incorrect use of the global variables! Much appreciated!
P.s: Pardon my naivety but, I did not exactly understand as to how the global variable mid would get re-written by the first recursive call. I mean, it is defined out side the function body of the mergesort routine right. I thought that globally defining and initializing it with zero would enable me to use it as the 'inv' global variable. Does it get re-written because it's address is accessible always any where in the program?
if last is 10 and first is 0, mid would be set to 5 and you will expect ypur functions to be called as:
1 2
mergesort(arr, 0, 5);
mergesort(arr, 6, 10);
But first recursive call to mergesort will execute same code snippet and set mid to 2. So, assuming no changes to mid would be done in deeper calls actually your second call is mergesort(arr, 2, 10);. And that call modifies mid too so your call to merge functions changes too. In the end you will try to merge two unsorted arrays which will lead to serious problems.