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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:16:21+00:00 2026-05-16T11:16:21+00:00

void merge(vector<int> dst,vector<int> first,vector<int> second) { int i=0,j=0; while(i<first.size()&&j<second.size()) { if(first[i]<second[j]) { dst.push_back(first[i]); i++;

  • 0
void merge(vector<int> dst,vector<int> first,vector<int> second)
{
    int i=0,j=0;

    while(i<first.size()&&j<second.size())
    {
        if(first[i]<second[j])
        {
            dst.push_back(first[i]);
            i++;
        }
        else
        {
            dst.push_back(second[j]);
            j++;
        }
    }
    while(i<first.size()
    dst.push_back(first[i++]);

    while(j<second.size())
    dst.push_back(second[j++]);
}

void mergeSort(vector<int> &a)
{   
    size_t sz = a.size();
    cin.get();
    if(sz>1)
    {   
        vector<int> first(&a[0],&a[sz/2]);
        vector<int> second(&a[(sz/2)+1],&a[sz-1]);

        mergeSort(first);
        mergeSort(second);

        merge(a,first,second);  
    }
}

void MergeSort(int* a,size_t size)
{
   vector<int> s(&a[0],&a[size-1]);
   mergeSort(s);
}

Can some one help me what is the problem with this code ? I am getting vector subscript outof range error.

  • 1 1 Answer
  • 2 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-05-16T11:16:22+00:00Added an answer on May 16, 2026 at 11:16 am

    Your sub vectors are specified incorrectly.
    Remember the iterators specify the beginning to one past the end.

    So this will misses the middle element and the last element in the vector.
    And is also undefined for really short vectors of length 2

        vector<int> first(&a[0],&a[sz/2]);
        vector<int> second(&a[(sz/2)+1],&a[sz-1]);
    

    Imagine if a is the vector { A, B, C, D}

        first:  {A,B}   0 -> 2 (where 2 is one past the end so index 0 and 1_
        second: {}      3 -> 3 (Since one past the end equals the start it is empty}
    

    Or Try a bigger vector: { A, B, C, D, E, F, G, H, I}

        first:  {A, B, C, D}    0 -> 4 (4 is one past the end so index 0,1,2,3)
        second: {F, G, H}       5 -> 8 (8 is one past the end so index 5,6,7)
    

    Or Try a smaller vector: { A, B}

        first:  {A}    0 -> 1
        second: {BANG} 2 -> 1
    

    Should be:

        int* st = &a[0];
        // Using pointer arithmatic because it was too late at night
        // to work out if &a[sz] is actually legal or not.
        vector<int> first (st,      st+sz/2]); // sz/2 Is one past the end.
        vector<int> second(st+sz/2, st+sz   ); // First element is sz/2  
                                               // one past the end is sz
    

    The vectors passed into merge(). The dst parameter has to be passed by reference as it is an out parameter. But also note that first and second parameters are const so we can pass by const reference (to avoid the copy step).

    void merge(vector<int>& dst,vector<int> const& first,vector<int> const& second)
    

    Also the merge function:

    Is pushing the value into dst. But dst is already full from the data that came in. So before we do the merge the destination must be cleared.

        mergeSort(first);
        mergeSort(second);
    
        // Must clear a before we start pushing stuff into.
        a.clear();   // Add this line.
        merge(a,first,second);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following is my implementation of merge sort. private static void mergeSort(int[] a, int
void foo(Node* p[], int size){ _uint64 arr_of_values[_MAX_THREADS]; for (int i=0 ; i < size
I have a merge sort and it will work when I do mergeSort<int>(val_array1,numValues); but
void merge(int *a1, int *a2, int *a3, int s1, int s2, int s3){ s2
Each time I use Merge() I have the following: 'Cannot implicitly convert type 'void'
- (void)TargetHit:(int)target{ void (^threadBlock)(void) = ^{ NSLog(@respond to selector %d, [self respondsToSelector:@selector(changeImageOfTarget:)]); [[NSOperationQueue mainQueue]
void say(char msg[]) { // using pointer to print out the first char of
I have written the following mergesort code. public class mergesort { public static int
using System; namespace MergeSort { class Program { static int[] vektor = { 5,
EDIT: When I merge link_def.cpp with link_dec.h, I only get the first error, not

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.