Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:06:17+00:00 2026-06-18T04:06:17+00:00

this is my mergesort function, I can not understand why am i getting always

  • 0

this is my mergesort function, I can not understand why am i getting always a “vector subscript out of range” error in VC++ express 2010.

vector<int> Ordinatore::doMerge(vector<int> &v1, vector<int> &v2) {
int dimV1 = v1.size();
int dimV2 = v2.size();

int dimTotale = dimV1 + dimV2;
vector<int> merged; 

int lh, rh;
lh=0;rh=0;


while(lh < dimV1)  {


    if (rh < dimV2 && v1[lh] <= v2[rh]) {

        merged.push_back(v1[lh]);

        lh++;
        /* the trouble should start here as far as I've seen with cout & debugging */
        while (rh < dimV2  && v2[rh] <= v1[lh]) {

            merged.push_back(v2[rh]);

            rh++;

        }

    }
    else {

        while (rh < dimV2  && v2[rh] <= v1[lh]) {
            merged.push_back(v2[rh]);
            rh++;
        }
        merged.push_back(v1[lh]);

        lh++;

    }
}

return merged;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:06:18+00:00Added an answer on June 18, 2026 at 4:06 am

    The problem is in the top of your loop:

    while(lh < dimV1)  {
        if (rh < dimV2 && v1[lh] <= v2[rh]) {
            merged.push_back(v1[lh]);
            lh++;                                       // |<-- Problem
            while (rh < dimV2  && v2[rh] <= v1[lh]) {   // |
               ...
            }
        }
        else {
            ...
            lh++;
        }
    }
    

    When that lh++ in the else is executed, lh is incremented and the while(lh < dimV1) restarts. Lets assume that it’s made it all the way to lh == dimV1 - 1…

    • If the if condition evaluates to true, lh++ is executed right were you’ve marked “the trouble should start here“.
    • The next line while (rh < dimV2 && v2[rh] <= v1[lh]) will cause a crash because lh now equals dimV1 but you’re using it to index v1.

    You can read more about Merge Sorts in this Wikipedia Article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting unexpected(?) results from this mergesort implementation. It's extremely slow compared to
I'm just not sure how to approach this problem. This is the error message
This code below allows me to find the word error in all my files
This works. $(document).ready(function(){ $(.items article).click(function(){ window.location=$(this).find(a).attr(href); return false; }); }); However , when the
I'm trying to understand the space requirements for a Mergesort, O(n). I see that
Can someone tell me if this is correct. I'm trying to overload my <<
This is not homework, I don't have money for school so I am teaching
I wrote a simple code for merge sort but it gave me this error:
i wrote this code in C language on Xcode following the algorithm of mergesort.
I have a mergesort function I am testing using JUnit. Below is one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.