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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:41:24+00:00 2026-06-03T01:41:24+00:00

I am trying to implement merge sort, but I cannot get it to work.

  • 0

I am trying to implement merge sort, but I cannot get it to work. I would be really grateful if anyone could find and point out the error in my thinking (and code).
Main function without unnecessary code:

int main(int argc, char *argv[]) {
    int n = 0;
    std::cin >> n;
    int num[n];

    for(int i = 0; i < n; i++) {
        std::cin >> num[i];
    }

    sort(num, &num[n - 1], n);

    return(0);
}

Merge sort function:

int *sort(int *s, int *e, int size) {
    if(s == e) {
        return(s);
    }

    int mid = (size + 1) / 2;

    //split array recursively to 1-element arrays

    int *left  = sort(s, (s + mid - 1), mid);
    int *right = sort(s + mid, e, size - mid);

    int *counter = s;

    //merge arrays back together

    while(left < (s + mid - 1) && right <= e) {
        //std::cout << *left << " " << *right << std::endl;

        for(; left < (s + mid - 1) && *left <= *right; left++, counter++) {
            *counter = *left;
        }

        for(; right <= e && *right <= *left; right++, counter++) {
            *counter = *right;
        }
    }

    for(; left < (s + mid - 1); left++, counter++) {
        *counter = *left;
    }
    for(; right <= e; right++, counter++) {
        *counter = *right;
    }

    return(s);
}

input0:
5
4 3 2 1 0
output0:
0 0 0 0 0

input1:
5
0 1 2 3 4
output1:
1 2 4 4 4

input2:
2
0 1
output2:
1 1

  • 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-03T01:41:26+00:00Added an answer on June 3, 2026 at 1:41 am

    Your problem seems to be at *counter = *left and *counter = *right.A usual implementation of the algorithm uses a separate array for creating the new merged array. But in this case , you are merging it inplace. When you do *counter = *left that value is lost and this probably leads to all the zeros you see in your sample run.

    When you are merging , you could do it in two ways. Either create a temporary array and keep on writing to it instead of the *counter and refill the actual array with the temporary array after you exhaust all the elements of both arrays, or resort to an insertion sort, once the array size to be merged is less than a predefined value .

    Both these methods are given in detail in
    http://en.literateprograms.org/Merge_sort_%28C%29

    PS:I also think you should change the condition left < (s + mid - 1) to left <=(s + mid -1) in all the places .

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

Sidebar

Related Questions

I'm trying to implement mergesort in Coldfusion, but it is spitting out incorrect results,
I am trying to write merge sort and stuck here. What is the problem
As part of a bigger implementation, I'm trying to implement a merge operation of
I'm trying to learn to work with Hibernate but probably i don't understand @ManyToOne
I am trying to implement a simple merge, without the use of anything pre-defined.
I am trying to implement a 'git-flow' kind of workflow using Gerrit but I
I'm trying to implement slow query logging into my database class, but I'm getting
I'm trying implement my project in Apache Struts 2 but I'm not very familiar
I am trying to implement my own map overlay for osmdroid (but I assume
I have been trying to implement various types of sort in a program am

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.