Whats wrong with this piece of code?

Posted by cambr on Stack Overflow See other posts from Stack Overflow or by cambr
Published on 2010-04-19T17:13:38Z Indexed on 2010/04/19 17:23 UTC
Read the original article Hit count: 338

Filed under:
|
|
vector<int>& mergesort(vector<int> &a) {
    if (a.size() == 1) return a;
    int middle = a.size() / 2;
    vector<int>::const_iterator first = a.begin();
    vector<int>::const_iterator mid = a.begin() + (middle - 1);
    vector<int>::const_iterator last = a.end();
    vector<int> ll(first, mid);
    vector<int> rr(mid, last);

    vector<int> l = mergesort(ll);
    vector<int> r = mergesort(rr);
    vector<int> result;
    result.reserve(a.size());
    int dp = 0, lp = 0, rp = 0;

    while (dp < a.size()) {
        if (lp == l.size()) {
            result[dp] = (r[rp]);
            rp++;
        } else if (rp == r.size()) {
            result[dp] = (l[lp]);
            lp++;
        } else if (l[lp] < r[rp]) {
            result[dp] = (l[lp]);
            lp++;
        } else {
            result[dp] = (r[rp]);
            rp++;
        }
        dp++;
    }
    a = result;
    return a;
}

It compiles coorectly but while execution, I am getting:

This application has requested the runtime to end it in an unusual way.

This is a weird error.

Is there something that is fundamentally wrong with the code?

© Stack Overflow or respective owner

Related posts about c++

Related posts about merge