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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:42:07+00:00 2026-05-26T04:42:07+00:00

First question here, and yes this is a homework question. We are tasked with

  • 0

First question here, and yes this is a homework question. We are tasked with performing merge sort on an array (which I am familiar with), but in a way I am unsure of how to do. Usually I would have a separate merge and merge sort function, and use the two. However, it sounds like he wants everything in one method? I was just hoping maybe someone could help clear things up for me, or put them into terms I can better understand.

From the assignment:

you will need to implement a non-recursive version of merge-sort
algorithm. Arrange two nested loops to accomplish this task. The outer
loop should provide the size of segments for merging. The inner loop
should take care of selecting positions of segments. The inner loop
should start at the left edge and move your segments to the right.
Arrange appropriate values of variables left, middle, right, so that
sorting is accomplished just by iterating the call
merge(a,left,middle,right).

I hate to be so vague, but I really don’t understand any of what he’s saying. First, what is meant by “outer loop should provide the size of segments”? How does a loop provide anything? What about the next sentence – what does he mean by segments? The data?

I’m not asking for code, but any psuedocode would be really helpful.

If anyone could try and decipher what he means, I’d appreciate it. I’ve already emailed him about it, but it’s been a few hours and I’ve yet to hear back.

Thank you!

  • 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-26T04:42:08+00:00Added an answer on May 26, 2026 at 4:42 am

    It’s not so difficult. Consider the recursive merge:

           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
                /     \               split
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
          /   \         /  \          split
      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
      / \     / \     / \     / \     split
    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
    | | | | | | | | | | | | | | | |
    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
      \ /     \ /     \ /     \ /     merge
      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
          \   /         \  /          merge
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
                \     /               merge
           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
    

    If you notice, when you split, you don’t really do anything. You just tell the recursive function to partially sort the array. Sorting the array consists of first sorting both halves and then merging it. So basically, what you have is this:

    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
    | | | | | | | | | | | | | | | |
    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
      \ /     \ /     \ /     \ /     merge
      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
          \   /         \  /          merge
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
                \     /               merge
           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
    

    Now from here it should be obvious. You first merge elements of the array 2 by 2, then 4 by 4, then 8 by 8 etc. That is the outer for gives you 2, 4, 8, 16, 32, … (which is what it calls size of the segment because the i of the loop contains that number) and the inner for (say with iterator j) goes over the array, i by i merging array[j...j+i/2-1] with array[j+i/2..j+i-1].

    I wouldn’t write the code since this is homework.

    Edit: a picture of how the inner for works

    Imagine if i is 4, so you are at this stage:

      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
          \   /         \  /          merge
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
    

    you will have a for that once gives you 0(which is 0*i) as j and then 4 (which is 1*i) as j. (if i was 2, you would have j going like 0, 2, 4, 6)

    Now, once you need to merge array[0..1] with array[2..3] (which is formulated by array[j..j+i/2-1] and array[j+i/2..j+i-1] with j = 0) and then array[4..5] with array[6..7] (which is formulated by the same formulas array[j...j+i/2-1] and array[j+i/2..j+i-1] because now j = 4) That is:

    i = 4:
           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
            ^ ^ ^ ^ ^ ^ ^ ^
            | | | | | | | |
           / / / /   \ \ \ \
         (j  =  0)   (j  =  4)
          | | | |     | | | |
          j | | |     j | | |
          | | | j+i-1 | | | j+i-1
          | | j+i/2   | | j+i/2
          | j+i/2-1   | j+i/2-1
          | | | |     | | | |
          | | | |     | | | |
          \ / \ /     \ / \ /
           v   v       v   v
           merge       merge
    

    Hope this is clear at least a little.


    Side help: Just a hint if you don’t really know how for works:

    for (statement1; condition; statement2)
    {
        // processing
    }
    

    is like writing

    statement1;
    while (condition)
    {
        // processing
        statement2;
    }
    

    So, if you always wrote

    for (int i = 0; i < 10; ++i)
    

    it meant starting from 0, while i is smaller than 10, do something with i and then increment it. Now if you want i to change differently, you just change statement2 such as:

    for (int i = 1; i < 1024; i *= 2)
    

    (Try to understand how that final for works based on its equivalent while that I wrote you)

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

Sidebar

Related Questions

Note 1: This is my first question here. Please be gentle. Note 2: Yes,
this is my first question here so I hope I can articulate it well
this is my first question here :) I know that I should not check
Greetings to all! This is my first question here on stackoverflow. I have a
Ok, I need help. This is my first question here. Background: I am working
this is my first question to stackoverflow so here it goes... I use cruise
This is related to this question here, but with a slight twist: instead of
Yes, this is probably yet another greatest-n-per-group question... But I've tried at least a
this is my first question here and if its format is not what is
Yes, this is a bit of a trick question; one array (without copies), as

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.