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

  • Home
  • SEARCH
  • 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 6770827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:21:33+00:00 2026-05-26T15:21:33+00:00

I found this example of merge_sort here at cprogramming . I’m tyring to break

  • 0

I found this example of merge_sort here at cprogramming. I’m tyring to break down the algorithm to understand the alogrithm with in the context of quick sort another SO post.

EDIT:

I’ve updated/fixed the code and posted it as an answer below. This is a working merge_sort.

  • 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-05-26T15:21:34+00:00Added an answer on May 26, 2026 at 3:21 pm

    The code posted here could use some improvements in readability…Here they are. Verified this is a working merge_sort:

    #include "c_arclib.cpp"
    void merge_helper(int *input, int left, int right, int *scratch)
      {
      if(right == left + 1)
        {
        return;
        }
      else
        {
        int i = 0;
        int length = right - left;
        int midpoint_distance = length/2;
        int l = left, r = left + midpoint_distance;
        merge_helper(input, left, left + midpoint_distance, scratch);
        merge_helper(input, left + midpoint_distance, right, scratch);
        for(i = 0; i < length; i++)
          {
          if((l < (left + midpoint_distance)) && (r == right || input[l] > input[r]))
            {
            scratch[i] = input[l];
            l++;
            }
          else
            {
            scratch[i] = input[r];
            r++;
            }
          }
        for(i = left; i < right; i++)
          {
          input[i] = scratch[i - left];
          }
        }
      }
    int merge_sort(int *input, int size)
      {
        int *scratch = (int *)malloc(size * sizeof(int));
        if(scratch != NULL)
        {
            merge_helper(input, 0, size, scratch);
            free(scratch);
            return 1;
        }
        else
        {
            return 0;
        }
      }
    
    int main()
      {
      int size=1000;
      int* array = new int[size]();
      util::rand_to_array(array,size);
      util::print_array(array, size);
      merge_sort(array, size);
      cout << endl; util::print_array(array, size);
      return 0;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I found this: http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html Here is the example: <definition name=myapp.homepage template=/layouts/classic.jsp> <put-attribute name=title
I found this example and used it How do you resize a Bitmap under
I found this example in the App Engine documentation . 1 <% 2 UserService
I found this command line search and replace example: find . -type f -print0
i'm using this example implementation found at http://tangentsoft.net/wskfaq/examples/basics/select-server.html This is doing most of what
This is my assignment question: Explain with an example quick sort , merge sort
I found this example where they used PowerMock and EasyMock to stub/mock the Menu
I have found this example on StackOverflow: var people = new List<Person> { new
Can someone please correct me, I've found this example online and bunch of others
Looking on the internet for C++ brainteasers, I found this example: #include <iostream> using

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.