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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:46:14+00:00 2026-06-07T07:46:14+00:00

void merge(int *a1, int *a2, int *a3, int s1, int s2, int s3){ s2

  • 0
 void merge(int *a1, int *a2, int *a3, int s1, int s2, int s3){
     s2 -=1;
     int track =0;
     s3-=1;
     int p2=0;
     int p3=0;
     while(p2<s2 && p3 <s3){
         if(a2[p2]<a3[p3])
             a1[track++]=a2[p2++];
         else
             a1[track++]=a3[p3++];
     }
     while(p2<s2) a1[track++]=a2[p2++];
     while(p3<s3) a1[track++]=a3[p3++];
 }


 void msort(int *array, int n){
     if(n<=1) return;
     int a1_size = n/2;
     int a2_size = n-n/2;
     int a1[n/2];
     int a2[n-n/2];
     for(int i=0;i<n/2;i++){
    a1[i] = array[i];
     }
     for(int i=0,j=n/2;j<n;i++,j++){
         a2[i] = array[j];
     }
     msort(a1, n/2);
     msort(a2, n-n/2);
     merge(array, a1, a2, n, a1_size, a2_size);
 }

 int main(){
     int a[]={1,2,1,2,3,94,5,67};
     msort(a,8);
     for(int i=0;i<8;i++)
         cout << a[i] << endl;
}

Here is my code, and i expect it will return a sorted array.
I know the problem is about passing the address of array to merge will ammend data inside array and hence affect the result. I tried for a long time but still cant figure out how to ammend it. Would someone can give me a hint or 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-07T07:46:16+00:00Added an answer on June 7, 2026 at 7:46 am

    The problem is in s2 -= 1 and s3 -= 1 statements. Why are you doing that? A standard idiom for iterating array from zero-based index is to count while index < length, not while index < length-1.

    A couple of other notes:

    • you allocate a lot of additional memory (n*log(n) ints) which stays unused most of the time. It is only necessary to allocate inside merge, that way at most n ints will be allocated at any time.
    • learn to use std::vector and/or iterator ranges instead of C-style arrays
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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++;
The following is my implementation of merge sort. private static void mergeSort(int[] a, int
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 foo(Node* p[], int size){ _uint64 arr_of_values[_MAX_THREADS]; for (int i=0 ; i < size
I need make a merge sort using an additional array. Here is my code:
I have a function like this: private void GetRSS(int start, int end) { for
Hi I'm trying to inflate my layout with <merge> tag here is my main.xml
/*Program to merge to arrays using pointers in descending order, when the first array
i wrote a regular merge sort array code and all i want to do

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.