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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:32:30+00:00 2026-05-26T11:32:30+00:00

As part of a homework assignment, I need to be able to implement a

  • 0

As part of a homework assignment, I need to be able to implement a merge sort using a structure as the main argument. Having not been familiar with the merge sort until today, i have attempted to write my own implementation of it. For some reason I cannot get it to work.

Here is my code:

#include<iostream>
#include<stdlib.h>

using namespace std;

struct MergeArgument
{
    int *numArray;
    int *tempArray;
    int lowIndex, highIndex;
};

void merge(MergeArgument*);
void merge_sort(MergeArgument*);

int main(int argc, char** argv)
{   int SIZE = 25;
    MergeArgument arg;
    int arr[SIZE];
    int temp[SIZE];

    for(int k = 0; k < SIZE; k++)
    {
        arr[k] = rand() % 100;
        cout << arr[k] << " ";
    }

    arg.numArray = arr;
    arg.tempArray = temp;
    arg.lowIndex = 0;
    arg.highIndex = SIZE - 1;

    cout << endl;

    merge_sort(&arg);

    cout << "Sorted array: \n";

    for (int i = 0; i < SIZE; i++)
        cout << arr[i] << " ";
    cout << endl;

    return 0;
}

void merge_sort(MergeArgument *arg)
{   int tempHigh, tempLow;
    if(arg->lowIndex < arg->highIndex)
    {
        tempHigh = arg->highIndex;
        tempLow = arg->lowIndex;
        arg->highIndex = (tempHigh + tempLow) / 2;
        merge_sort(arg);
        arg->highIndex = tempHigh;
        arg->lowIndex = ((tempHigh + tempLow) / 2) + 1;
        merge_sort(arg);
        arg->lowIndex = tempLow;
        merge(arg);
    }

}

void merge(MergeArgument *arg)
{   int low = arg->lowIndex, mid = ((arg->lowIndex + arg->highIndex) / 2), high = arg->highIndex;
    int i = low, lowCounter = low, highCounter = mid + 1;

    while((lowCounter <= mid) && (highCounter <= high))
    {
        if(arg->numArray[lowCounter] < arg->numArray[highCounter])
        {
            arg->tempArray[i] = arg->numArray[lowCounter];
            lowCounter++;
        }
        else
        {
            arg->tempArray[i] = arg->numArray[highCounter];
            highCounter++;
        }
        i++;
    }

    if (lowCounter < mid)
    {
        for (int k = lowCounter; k < mid; k++)
        {
            arg->tempArray[i] = arg->numArray[k];
            i++;
        }
    }
    else
    {
        for (int k = highCounter; k <= arg->highIndex; k++)
        {
            arg->tempArray[i] = arg->numArray[k];
            i++;
        }

    }

    for(int k = arg->lowIndex; k <= arg->highIndex; k++)
    {
        arg->numArray[k] = arg->tempArray[k];
    }
}

Here is the output I am getting:

83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 11 68 67 29 82    
Sorted array:  
11 -1216235240 15 0 21 26 -1079135248 26 27 -1079135396 29 -1216770650 35 -1216235240 49 -1216492084 59 0 68 72 82 83 0 86 82

Can anyone point out what exactly I am doing wrong?

  • 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-26T11:32:30+00:00Added an answer on May 26, 2026 at 11:32 am

    This is pretty close to working, although you might want to consider some of the comments folks have made on making this more C++ like. No doubt this is from hard-won experience that there is never enough time to go back and do what you really should do.

    The problem I see is here, in merge:

    if (lowCounter < mid)
    {
        for (int k = lowCounter; k < mid; k++)
        {
            arg->tempArray[i] = arg->numArray[k];
            i++;
        }
    }
    

    You might want to compare and contrast the bounds here to the initial loop.

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

Sidebar

Related Questions

so this is part of my homework assignment. need some help with it. my
I need to implement a digraph(Directed graph) in c++ as part of a homework
As part of a homework assignment in Lisp, I am to use apply or
This is part of a homework assignment. I've got several questions asking find the
(this is indirectly a part of a much larger homework assignment) I have something
As part of a homework assignment, I have to program a simple chess game
I need little help on a homework assignment. I have to create a 10
as a part of a homework assignment, I'm trying to read a single char
For a homework assignment in linear algebra, I have solved the following equation using
I've been asked (as part of homework) to design a Java program that does

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.