Search code examples
c++recursionoutputmergesort

merge sort c exception OOR UPD::output problems


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.


Solution

  • 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.