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 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

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
In the YouTube API docs I found this example : https://gdata.youtube.com/feeds/api/videos?q=surfing&max-results=10&fields=entry[yt:statistics/@viewCount>1000000] Which, of course,
Found this is some example CSS I was reading and I'm having a hard
I found this command line search and replace example: find . -type f -print0
Example: We found this is some vendor written code and we're trying to figure
I have started learning HTTPUNIT and found one basic example. In this example it
like in this post here by exemple I have found this blog but in
I found this example of Custom LinearLayout on stackoverflow but it throws errors when
I found this example on the MySQL Tutorial: SELECT article, dealer, price FROM shop

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.