It would have been better if you had used iterators to implement the sort. ItsIt's a lot more versatile than using a specific container.
38 int_v::iterator m = v.begin() + v.size()/2; 39 int_v l(v.begin(), m); 40 int_v r(m, v.end());
int_v::iterator m = v.begin() + v.size()/2;
int_v l(v.begin(), m);
int_v r(m, v.end());
Each recursive call makemakes a copy of the data to be merged. So we get this progression.:
So you are using 2n\$2n\$ size of data just through calling MergeSortMergeSort
and it is being allocated and deallocated as the std::vectorstd::vector
is being used then destroyed on return.
In Addition you are doing another memory allocation in Merge()
:
Note:
By using iterators rather than containers. It, it becomes easy to avoid some of this copying. You just pass the ranges of the containers and do it in-place.
Prefer to move rather than to copy.
30 vec = res;
Here you are copying the contentcontents of the array. Arrays can be moved (this means it swaps a couple of pointers rather than copying all the data). Since you are not using res
after this point you should move it.
30 vec = std::move(res);
User defined types usually start with a capitolcapital
To distinguish user defined types and objects. Proceed types with an initial capitolcapital letter.
Personal
I hated that you posted line numbers into the question.
That kind of things makes it hard to cut and paste your code into our editors for review.
Also if SE sites decide to add line numbers to their code your extra line numbers will become an eye sore.