Ecco in questo modo funziona:
// Mergesort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int n;
int counter = 0;
void merge(vector<int> num, int left, int center, int right)
{
vector<int> nr;
nr.resize(num.size());
int i, j;
for (i = center+1; i > left; i--)
{
nr[i] = num [i-1];
for (j = center; j < right; j++ )
{
//nr[right+center-j] = num [j+1];
nr[j] = num[j+1];
for(int m = left; m <= right; m++)
{
if (nr[j] < num [i])
{
num[m] = nr[j-1];
counter= counter+1;
}
else
{
num[m] = nr[j+1];
counter=counter+1;
}
}
}
}
}
static vector<int> mergesort(vector<int> num, int left, int right)
{
if(right>left)
{
int center = (left+right)/2;
mergesort(num, left, center);
mergesort(num, center+1, right);
merge(num, left, center, right);
}
return num;
}
int main()
{
// read input
int t;
int h;
for(cin >> t; t > 0; --t)
{
cin >> n;
vector<int> numbers; // create a vector containing the subsequence
for(int i = 0; i < n; i++)
{
cin >> h;
numbers.push_back(h);
}
// operations with the current sequence starts here:
numbers = mergesort(numbers, 0, n-1);
//------Debug Process----------------------------
cout << counter;
cout << " " ;
for (int z = 0; z < n; z++)
{
cout << numbers[z];
cout << " ";
}
cout << endl;
}
int end;
cin >> end;
}
il problema adesso é che non mi stampa la lista in ordine crescente...