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

The Archive Base Latest Questions

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

I’m having trouble printing out a MergeSort. I need help printing out each step

  • 0

I’m having trouble printing out a MergeSort. I need help printing out each step by step process as it’s sorting an ArrayList.

The following example is from a InsertionSort, as I had it print at every single time it swapped two elements in the ArrayList:

11 79 60 45 START

11 79 60 45

11 60 79 45

11 45 60 79 FINISH

Is there anyway to do this for MergeSort while showing the entire array from start to finish (like above?)

Code:

import java.util.ArrayList;

public class Merge 
{
    public static void main (String [] args) 
    {
        Merge run = new Merge();
        run.test();
    }

    public void test ( )
    {
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        for (int i = 0; i < 16; i++)
        {
            numbers.add(new Integer(1 + (int)(Math.random() * 100)));
        }
        printArray(numbers);
        mergeSort(numbers);
        printArray(numbers);
    }

    public void printArray (ArrayList<Integer> array)
    {
        System.out.println("\n\n");
        for (int i = 0; i < array.size(); i++)
        {
            System.out.printf("%-5d",array.get(i).intValue());
        }
        System.out.println("\n\n");
    }

    public void mergeSort (ArrayList<Integer> array) 
    {   
        int length = array.size();
        if (length < 2)
        {
            return;  // the array is already sorted in this case
        }
        // divide
        ArrayList<Integer> array1 = new ArrayList<Integer>(); 
        ArrayList<Integer> array2 = new ArrayList<Integer>(); 
        int i = 0;

        while (i < length/2)
        {
            array1.add(array.remove(0)); // move the first n/2 elements to array1
            i++;
        }
        while (!array.isEmpty())
        {
            array2.add(array.remove(0)); // move the rest to array2
        }

        mergeSort(array1);
        mergeSort(array2);
        merge(array1,array2,array); 
    }

    public void merge (ArrayList<Integer> array1, ArrayList<Integer> array2, ArrayList<Integer> array)
    {   
        while (!array1.isEmpty() && !array2.isEmpty())
        {
            if ((array1.get(0).compareTo(array2.get(0)) <= 0))
            {
                array.add(array1.remove(0));
            }
            else
            {
                array.add(array2.remove(0));
            }
        }
        while(!array1.isEmpty()) // move the remaining elements of array1
        {
            array.add(array1.remove(0));
        }
        while(!array2.isEmpty()) // move the remaining elements of array2
        {
            array.add(array2.remove(0));
        }
    }
}
  • 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-27T09:48:42+00:00Added an answer on May 27, 2026 at 9:48 am

    If you passed some offset to mergeSort, you may be able to print the sub-array indented over to where it would be in the full array as you make swaps, but since you are only passing portions of the array down, you won’t be able to show the full array in this manner. However, there is a faster way that would allow you to.

    Instead of making new arrays and passing them down, pass the array and 2 indices, the begin and end point. So you say mergeSort(array, 0, n) for the first, then mergeSort(array, 0, n/2) and mergeSort(array, n/2, n) for the recursive calls. You do your splitting and merging only within those bounds. Then as you merge, you can print out the whole merged array. This would show the step at each merge. At the bottom level, it would show the 1-1 swap (if it occurs). That’s the only “step-by-step” you could see in a merge sort.

    • 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'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
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
I need to clean up various Word 'smart' characters in user input, including but

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.