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

  • SEARCH
  • Home
  • 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 9212069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:26:30+00:00 2026-06-18T01:26:30+00:00

While trying to make this merge-sort algorithm with recursive calls, i ended up getting

  • 0

While trying to make this merge-sort algorithm with recursive calls, i ended up getting an exception of std::out_of_range.

I don’t know much about debugging and finding causes of errors. Below i will post the code(not full, only some parts) and a sourcefile containing the same code(full version, of course).

I will gladly listen to suggestions, even if they don’t provide any help against this error, so feel free to comment this code and make jokes of me 🙂

https://docs.google.com/file/d/0ByVN9ccAyFY2dkVLN0ZlTWVHZG8/edit

Main func

int main()
{
vector<int> original;   //input vector
input (&original);      //write input to vector<int> original

divide(&original);      //pass the vector

for(unsigned int i=0;i<original.size();i++)//output the results
cout<<original.at(i);
}

Input func

    int input(vector<int> *inVec) //read all input until non-integer
{
int tmp;
while (cin>>tmp)
inVec->push_back(tmp);
for(unsigned int i=0;i<inVec->size();i++)
cout<<inVec->at(i)<<endl;
}

Divide

int divide(vector<int> *original)
{
int origL=original->size();
if(origL>1)
{
vector<int> first;      //vectors for holding 2 halfs of "original"
vector<int> second;     //

first.assign(original->begin(),original->begin()+origL/2);//1st half of "original"
second.assign(original->begin()+origL/2+1,original->end());//2nd half

divide(&first);     //recursive call until "first" and
divide(&second);    //"second" include only one number

merge(&first,&second,original);//merge first and second back into one vector
}
}

Merge func

int merge(vector<int> *A,vector<int> *B,vector<int> *original)
{
//clear the original vector. we will use it to store sorted results.
original->erase(original->begin(),original->end());
unsigned int i=0,j=0;                                   

//out the smallest number from A and B into
//original[0] and so on. This makes it a 
//sorting algorithm.
for(i=0;i<A->size();i++)
{
if(j<B->size())
    if(A->at(i)<=B->at(j))
        original->push_back(A->at(i));
    else{
        original->push_back(B->at(j));
        i--;j++;}
}
//the ABOVE loop scans whole vector A or B.
//if there are still uncopied elements in
//the other vector, then we check for them and
//push them into original.
if(j<B->size())
    for(i=j;i<B->size();i++)
        original->push_back(B->at(i));
if(i<A->size())
    for(j=i;j<A->size();j++)
        original->push_back(A->at(j));


return EXIT_SUCCESS;
}

EDIT1:
Made the changes to MERGE, so now there are no runtime errors. However, output is not right. If someone notices what could cause the problem, please kindly tell me. Meanwhile I am going to try to find it myself.

  • 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-18T01:26:31+00:00Added an answer on June 18, 2026 at 1:26 am

    What will happen when you run out of elements in B in the function merge? OOR. Try a test case when all the elements in B are smaller than the ones in A and only call merge to see what I mean.

    Also this is c++ please use reference in preference to pointers.

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

Sidebar

Related Questions

Getting this error on chrome while trying to make cross-domain requests with GWT application.
I am having this problem An error occurred while trying to make a request
Hi everyone i've been trying to make this work for quite a while and
Got stuck while trying to make an element in website behave like this: <div
I'm trying to make this work, I don't understand why it doesn't work since
I've been trying to make this work for a while now with no chance,
Hi since 3 hour I am trying to make this work but not getting
I've been trying to make this work for a while and it never seems
After trying to make a while(bool) loop and it failing because I couldn't see
Im trying to make a basic while loop example im preperation for my next

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.