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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:17:42+00:00 2026-05-27T05:17:42+00:00

I’m having some trouble with an assignment where I was to covert a specific

  • 0

I’m having some trouble with an assignment where I was to covert a specific recursive merge sort algorithm which uses a vector, but using an array instead.

Here’s what I have so far, Sort() works fine I believe. But, Merge() is where I think the issue lies…thank you!

#include "genlib.h"
#include <iostream>

/* Private function prototypes */
void Sort(int arr[], int n);
void Merge(int *arr[], int *arr1[], int *arr2[], int n1, int n2);

/* Main program */

int main() {
    int arr[] = {88, 10, 20, 50, 7, 44, 99, 9, 900, 44};
    int n = sizeof(arr) / sizeof(arr[0]);   

    Sort(arr, n);

    cout << "[";
    for (int i = 0; i < n; i++) {           // prints out sorted array
        if (i > 0) cout << ", ";
        cout << arr[i];
    }
    cout << "]" << endl;
    return 0;
}


/*
* Function: Sort
* Usage: void MergeSort(int arr[], const int START, const int END);
* ----------------------------------------------------------------------------------
* This function sorts the elements of the array into increasing numerical order 
* using the merge sort algorithm, which consists of the following steps:
*   1. Divide the array into two halves.
*   2. Sort each of these smaller array recursively.
*   3. Merge the two arrays back into the original one.
*
* NOTE: Although in the book, the creation of the 2 divided arrays would occur in 
*       this function, I had a lot of difficulty trying to get the recursive sort
*       to work with dynamic arrays. This is due to my inability to delete the 
*       array from memory before it gets called again.
*       I opted to dynamically create the array in Merge() instead.
*/

void Sort(int arr[], int n) {

    if (n <= 1) return; // base case

    int mid = n/2;

    int *arr1 = NULL; 
    int *arr2 = NULL; 

    arr1 = new int[mid];
    arr2 = new int[n-mid];

    for (int i=0; i<n; i++) {
        if (i < (mid)) {
            arr1[i] = arr[i];
        } else {
            arr2[i-(mid)] = arr[i];
        }
    }

    Sort(arr1,mid);
    delete[] arr1;
    Sort(arr2,n-mid);
    delete[] arr2;
    for (int i=0; i<n; i++) {
        arr[i] = 0;
    }
    Merge(&arr, &arr1, &arr2, n/2, n/2);
}

/*
* Function: Merge
* Usage: void Merge(int arr[], const int START, const int MID, const int END);
* ----------------------------------------------------------------------------------
* This function merges two sorted arrays into the original array, which should be 
* empty before this operation. Because the input arrays are sorted, the 
* implementation can always select the first unused element in one of the input 
* array vectors to fill the next position.
*/

void Merge(int *arr[], int *arr1[], int *arr2[], int n1, int n2) {
    int p1 = 0;
    int p2 = 0;

    while (p1 < n1 && p2 < n2) {
        if (arr1[p1] < arr2[p2]) {
            arr[p1+p2] = arr1[p1];
            p1++;
        } else {
            arr[p1+p2] = arr2[p2];
            p2++;
        }
    }
    while (p1 < n1) { 
        arr[p1+p2] = arr1[p1];
        p1++;
    }
    while (p2 < n2) { 
        arr[p1+p2] = arr2[p2];
        p2++;
    }
}

Here is the original vector implementation:

/*
 * Function: Sort
 * -------------- * This function sorts the elements of the vector into
 * increasing numerical order using the merge sort algorithm,
 * which consists of the following steps:
 *
 * 1. Divide the vector into two halves.
 * 2. Sort each of these smaller vectors recursively.
 * 3. Merge the two vectors back into the original one.
 */
void Sort(Vector<int> & vec) {
    int n = vec.size();
    if (n <= 1) return;
    Vector<int> v1;
    Vector<int> v2;
    for (int i = 0; i < n; i++) {
        if (i < n / 2) {
            v1.add(vec[i]);
        } else {
        v2.add(vec[i]);
        }
    }
    Sort(v1);
    Sort(v2);
    vec.clear();
    Merge(vec, v1, v2);
}

/*
 * Function: Merge
 * --------------- * This function merges two sorted vectors (v1 and v2) into the
 * vector vec, which should be empty before this operation.
 * Because the input vectors are sorted, the implementation can
 * always select the first unused element in one of the input
 * vectors to fill the next position.
 */
void Merge(Vector<int> & vec, Vector<int> & v1, Vector<int> & v2) {
    int n1 = v1.size();
    int n2 = v2.size();
    int p1 = 0;
    int p2 = 0;
    while (p1 < n1 && p2 < n2) {
        if (v1[p1] < v2[p2]) {
            vec.add(v1[p1++]);
        } else {
        vec.add(v2[p2++]);
        }
    }
    while (p1 < n1) vec.add(v1[p1++]);
    while (p2 < n2) vec.add(v2[p2++]);
}
  • 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-27T05:17:43+00:00Added an answer on May 27, 2026 at 5:17 am

    The issue is in “Sort”:

    Sort(arr1,mid);
    delete[] arr1;
    Sort(arr2,n-mid);
    delete[] arr2;
    for (int i=0; i<n; i++) {
        arr[i] = 0;
    }
    Merge(&arr, &arr1, &arr2, n/2, n/2);
    

    First you sort an array, then you Delete it (resulting arr1 is a null-Pointer)
    the same thing you do with arr2.

    When you start to merge it, you already have deleted the data and released the memory

    A solution could be, to move the “delete”-statements below the Merge-call:

    Sort(arr1,mid);
    Sort(arr2,n-mid);
    for (int i=0; i<n; i++) {
        arr[i] = 0;
    }
    Merge(&arr, &arr1, &arr2, n/2, n/2);
    delete[] arr1;
    delete[] arr2;
    

    This way you dont loose your data.
    Because you create your Array with a “new” – statement, you should not try to leave the “delete”‘s away.

    Hope this solves your Problem

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.