Merge sort algorithm implementation using C++ (redux) This is a follow up question to the one I asked a couple of days ago.



I reviewed the suggestions &amp; did some research (specifically, read relevant info on CLRS). The new version is 3 times faster (210ms vs 71ms on 100,000 numbers) &amp; looks cleaner.

One thing I still feel weird about is not using iterators except than to init the sub-vectors. However, any attempt in using them causes a drop in performance (down to 120ms).

I'm also curious if my code is good in terms of code style. Is this idiomatic C++?

void merge(std::vector&lt;int&gt;&amp; a, int p, int q, int r)
{
    std::vector&lt;int&gt; l(a.begin() + p, a.begin() + q + 1);
    std::vector&lt;int&gt; ri(a.begin() + q + 1, a.begin() + r + 1);

    int n1 = q - p + 1;
    int n2 = r - q;

    int i, j; i = j = 0;

    while (i &lt; n1 &amp;&amp; j &lt; n2) {
        if (l[i] &lt; ri[j]) {
            a[p++] = l[i++];
        } else {
            a[p++] = ri[j++];
        }
    }

    while (i &lt; n1) {
        a[p++] = l[i++];
    }

    while (j &lt; n2) {
        a[p++] = ri[j++];
    }
}

void merge_sort(std::vector&lt;int&gt;&amp; a, int p, int r)
{
    if (p &lt; r) {
        int q = (p + r) / 2;
        merge_sort(a, p, q);
        merge_sort(a, q + 1, r);
        merge(a, p, q, r);
    }
}
The first thing I would suggest is using better names for your variables.  It's very hard for someone else looking at or using your code to determine what those variables are supposed to represent.