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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:22:56+00:00 2026-06-09T23:22:56+00:00

I followed this recursive algorithm for merge sort detailed on Wikipedia. Here is the

  • 0

I followed this recursive algorithm for merge sort detailed on Wikipedia.

Here is the code that I came up with:

int* merge(int left[], int leftSize, int right[], int rightSize){
int result[leftSize + rightSize];   //The merged array
int resultPointer = 0;  //Index position of where to insert element
int leftPointer = 0;
int rightPointer = 0;

//While length of either of the lists is > 0
while((leftSize > 0) || (rightSize > 0)){

    cout << "Got here" << endl;
    //If length of both left and right lists is > 0
    if((leftSize > 0) && (rightSize > 0)){

        //Compare first elements from both lists and put smallest one in the result list
        if(left[0] < right[0]){
            result[resultPointer] = left[0];
            leftPointer++;
            leftSize--;
        }else if(right[0] < left[0]){
            result[resultPointer] = right[0];
            rightPointer++;
            rightSize--;
        }else{
            //if both elements are the same, put them both in the result list
            result[resultPointer] = left[0];
            leftPointer++;
            leftSize--;
            result[resultPointer++] = right[0];
            rightPointer++;
            rightSize--;
        }
        resultPointer++;    //Increment pointer to point to next empty element

    }else if(leftSize > 0){
        result[resultPointer] = left[0];
        leftPointer++;
        leftSize--;
    }else if(rightSize > 0){
        result[resultPointer] = right[0];
        rightPointer++;
        rightSize--;
    }


}

//int* resultList = result;

return result;
}

int* merge_sort(int list[], int size){

//If list has 1 element then it is sorted so just return that
if(size<=1){
    return list;
}

int middle = size/2;    //Get mid point of given list

//Create left and right arrays
int left[middle];
int right[size-middle];

for(int i = 0; i<size-middle; i++){

    if(i<middle){
        left[i] = list[i];
    }
    right[i] = list[i+middle];
}

//Recursively call merge sort to sort out the sublists
int* leftList = merge_sort(left, middle);
int* rightList = merge_sort(right, size-middle);

//Merge the sorted lists and return a fully sorted list

int* merged = merge(leftList, middle, rightList, size-middle);

return merged;
}

The resulting output is just a bunch of random numbers(which I think happens when something hasn’t been initialized). This implementation might be stupid but I would still like to know what is going wrong here.

Cheers

  • 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-09T23:22:58+00:00Added an answer on June 9, 2026 at 11:22 pm

    You are returning a pointer from your merge function which points to a local variable. The local variable will go out of scope the moment you return from your merge function. So you return a pointer that is not pointing to any valid memory.

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

Sidebar

Related Questions

I followed this post http://neurochannels.blogspot.com/2010/05/how-to-run-r-code-in-matlab.html , to install R(D)Com server in order to call
I followed this description for debugging native code in an Android app and it
I followed this tutorial: http://codeigniter.com/wiki/Internationalization_and_the_Template_Parser_Class/ The controller that loads the language is this one:
I followed this tutorial and the source code : http://blog.objectgraph.com/index.php/2010/04/20/encrypting-decrypting-base64-encode-decode-in-iphone-objective-c/ And then, I downloaded
I followed this post here and sorted out how to set the ForeignKey of
I followed this tutorial to enable single sign on for an app that installs
I followed this tutorial on configuring the Rails plugin ExceptionNotifier. I know that I
Followed this question about delayed_job and monit Its working on my development machine. But
I followed this tutorial . But whenever I try to log in with my
I followed this video tutorial for detecting memory leaks using Instruments with Xcode 4.3.2.

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.