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 9217025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:32:53+00:00 2026-06-18T02:32:53+00:00

Update: modified condition in the merge function, now everything works fine I’m trying to

  • 0

Update: modified condition in the merge function, now everything works fine

I’m trying to implement a merge sort algorithm in c++ which works with intege vectors. So here is my piece of code:

#include <vector>
#include <iostream>

using namespace std;

void Vout (vector <int> V){
    for (int i = 0; i<V.size(); i++)
        cout << V[i] << "\t";
    cout << endl;
}

vector <int> merge (vector <int> B, vector <int> C){
    vector <int> D;
    int n = B.size() + C.size();
    int i=0, j=0;

    for (int k = 0; k<n; k++){
        if( ( B[i]<C[j] || j == C.size() ) && i<B.size() ){  //was like this: if( B[i]<C[j] ){
            D.push_back(B[i]);
            i++;
        }
        else{
            D.push_back(C[j]);
            j++;
        }
    }
    return D;
}

vector <int> merge_sort(vector <int> A){
    int n = A.size();
    if (n<=1) return A;
    vector <int> B, C;
    for (int i = 0; i<n/2; i++)
        B.push_back(A[i]);
    for (int i = n/2; i<n; i++)
        C.push_back(A[i]);

    B = merge_sort(B);
    C = merge_sort(C);
    vector <int> D = merge(B, C);
    return D;
}


int main() {
    vector <int> data;
    freopen("input.txt", "rt", stdin);
    int n;
    while (cin >> n){
        data.push_back(n);}
    Vout (data);
    Vout (merge_sort(data));
}

However with this input

5 4 6 7 9 1 2 8 3 12 10

it gives the following output:

1 2 3 0 4 5 6 7 8 9 0

I don’t quite understand where all these zeroes came from. Please help.

  • 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-18T02:32:55+00:00Added an answer on June 18, 2026 at 2:32 am

    Your merge() function doesn’t handle running off the end of the B or C vectors; when it reaches the end of either, your comparison (B[i]<C[j]) will be comparing against one past the end of the array.

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

Sidebar

Related Questions

UPDATE: Based on suggestions here, I've modified my code. To get a handle on
Update: Is there a way to achieve what I'm trying to do in an
Update : This is no longer an issue from C# 6, which has introduced
I want to update a record in a table but based on a condition
How to create a cron job in PHP for getting the update of modified
I'm trying to get my first ever trigger and function to work, but how
Update: Modified title to better reflect my question Hi everybody :) My question today
Update : Well, now I've gone and done it: I filed a bug report
** Update ** Modified the code for Ricardo Lohmann's suggestion with the additional modification
Is there a way to update a modified plist file that's in the bundle

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.