Recursive Implementation of Merge Sort C++ I am currently reading an algorithms book and I read on the insertion and merge sorts. I implemented the merge sort in C++, The first function MERGE is responsible for the merging of the two subarrays part and the function MERGE_SORT is the actual algorithm. I am interested to know whether or not my code has a good readability factor and if It can be optimized further or not?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;limits&gt;

using namespace std;

#define itr vector&lt;int&gt;::iterator
void MERGE(vector&lt;int&gt;&amp; vec,itr left, itr mid, itr right){
    //Make vector left with elements from left to mid inclusive
    vector&lt;int&gt; left_vec;
    for (itr i=left; i!=vec.end()&amp;&amp;i&lt;=mid;i++){
        left_vec.push_back(*i);
    }
    left_vec.push_back(numeric_limits&lt;int&gt;::max());//sentinel card
    //make vector right with elements from mid+1 to right inclusive
    vector&lt;int&gt; right_vec;
    for (itr i=mid+1; i!=vec.end() &amp;&amp; i&lt;=right; i++){
        right_vec.push_back(*i);
    }
    right_vec.push_back(numeric_limits&lt;int&gt;::max());//sentinel card
    //Now add them in a sorted manner to vector from left to right

    itr l=left_vec.begin(); itr r=right_vec.begin();
    for (itr vec_itr=left;vec_itr!=vec.end()&amp;&amp;vec_itr&lt;=right;vec_itr++){
        if (*l&lt;=*r){
            *vec_itr=*l;
            l++;
        }
        else{
            *vec_itr = *r;
            r++;
        }
    }
}
void MERGE_SORT(vector&lt;int&gt;&amp; vec, itr left, itr right){
    if(left&lt;right){
        itr mid = left + (right-left)/2;
        MERGE_SORT(vec,left,mid);
        MERGE_SORT(vec,mid+1,right);
        MERGE(vec, left,mid,right);
    }
    return;
}
int main() {
    int a[] = {10,8,9,7,6,3,3,9,15,4,3,2,1};
    vector&lt;int&gt; vec(a,a+13);
    MERGE_SORT(vec,vec.begin(),vec.end());
    for (itr i=vec.begin();i!=vec.end();i++)cout &lt;&lt; *i &lt;&lt; " ";
    return 0;
}
First off, I'd use typedef rather than #define.

You don't really need to reference vec.end(); you know mid &lt;= right &lt;= vec.end() and the fact that you're copying the same number of elements back in as you copied out, means you don't need to compare vec_itr to vec.end(). What this really means is that you don't need the vec parameter to MERGE.

vector&lt;T&gt; has a constructor that copies from an iterator. Your push_back for loop could be simplified to

vector&lt;int&gt; left_vec(left, mid);
vector&lt;int&gt; right_vec(mid, right);


The push_back of max() as a sentinel is not strictly needed. Instead, you can just loop until you hit the end of either left_vec or right_vec and then copy the remainder of the vectors to the left to right range.

Your variable names might be easier to understand.

typedef itr vector&lt;int&gt;::iterator;

void MERGE(itr left, itr mid, itr right){
    //Make vector left with elements from left to mid-1 inclusive
    vector&lt;int&gt; left(left, mid);

    //make vector right with elements from mid to right-1 inclusive
    vector&lt;int&gt; right(mid, right);

    // Now concurrently loop over the left and right vectors
    // and add the values back to left to right. Stop when 
    // either iterator runs out of elements.
    itr left_src=left.begin();
    itr right_src=right.begin();
    itr dest = left;
    while (left_src != left.end() &amp;&amp; right_src != right.end()) {
        // pick the iterator with the smallest current value
        itr&amp; least_src = (*left_src &lt; *right_src) ? left_src : right_src;
        *dest = *least_src;
        least_src ++;
        dest ++;
    }

    // copy remainder of left (maybe 0 elements)
    copy(left_src, left.end(), dest);

    // copy remainder of right (maybe 0 elements)
    copy(right_src, right.end(), dest);
}