1
#include <iostream>
#include<vector>
std::vector<int> print(std::vector<int> answerVec){
    for(int i = 0; i < answerVec.size(); i++){
        std::cout<<answerVec.at(i)<<" ";
    }
    std::cout<<"\n";
    return answerVec;
}
std::vector<int> Merge(std::vector<int> left, std::vector<int> right){
    int leftSize = left.size();
    int rightSize = right.size();
    std::vector<int> finalVector;
    int leftIterator = 0, rightIterator = 0;
    while(leftIterator < leftSize && rightIterator < rightSize){
        //std::cout<<"Left"<<left.at(leftIterator)<<"\n";
        //std::cout<<"Right"<<right.at(rightIterator)<<"\n";
        if((int)left.at(leftIterator) <= (int)right.at(leftIterator)){
            finalVector.push_back(left.at(leftIterator));
            leftIterator ++;
        }
        else{
            finalVector.push_back(right.at(rightIterator));
            rightIterator++;
        }
        //print(finalVector);
    }
    while(leftIterator < leftSize){
        finalVector.push_back(left.at(leftIterator));
        leftIterator ++;
        //print(finalVector);
    }
    while(rightIterator < rightSize){
        finalVector.push_back(right.at(rightIterator));
        rightIterator++;
        //print(finalVector);
    }
    return finalVector;
}
int main(){
    std::vector<int> vecL,vecR,vec;
    int n,input;
    std::cin>>n;
    for(int i = 0; i < n; i++){
        std::cin>>input;
        vecL.push_back(input);
    }
    for(int i = 0; i < n; i ++){
        std::cin>>input;
        vecR.push_back(input);
    }
    vec = Merge(vecL, vecR);
    for(int i = 0; i < vec.size(); i++){
        std::cout<<vec.at(i)<<" ";
    }
    return 0;
}

The Merge function above is not functioning properly when I am passing two sorted vectors as argument. Have tried for a while now but cannot make it work. Have commented some print statements that I was using for debugging. Any help would be much appreciated.

Thanks in advance

1 Answer 1

1
if((int)left.at(leftIterator) <= (int)right.at(leftIterator)){

Here, the second leftIterator should be rightIterator.

11
  • 1
    One of those days when you want to bang your head against the wall after seeing your mistake...Thanks Commented Apr 5, 2018 at 10:23
  • @SandeepWalia The good next question is how to prevent this kind of stupid bug, but in this particular case, I sadly don't have a compelling answer.
    – Gassa
    Commented Apr 5, 2018 at 10:25
  • One fix might be to use const iterators instead of indices? That way left and right are only mentioned once.
    – Gem Taylor
    Commented Apr 5, 2018 at 10:32
  • As a minor refinement, please reserve on your final vector to save it resizing?
    – Gem Taylor
    Commented Apr 5, 2018 at 10:33
  • @GemTaylor Ah, right! The real iterator (as opposed to integer index) is one mention of both the vector and the position in it.
    – Gassa
    Commented Apr 5, 2018 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.