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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:15:34+00:00 2026-06-02T18:15:34+00:00

I have a class that does some recursive merge sorting on a generic List,

  • 0

I have a class that does some recursive merge sorting on a generic List, as long as the element implement Comparable. I have a void method called mergeSort(List toSort), and a method mergeSortedLists(List left, List right) that takes two lists that have already been sorted, and then combines them into one sorted list. The problem is, the mergeSort(…) method doesn’t seem to be manipulating the toSort variable. Well it is, but the changes don’t show after it jumps up a level. Here are the sorting methods:

public static <E extends Comparable<E>> void mergeSort(List<E> toSort)
{
  if(toSort.size() > 1)
  {
    List<E> temp = toSort.subList(0, toSort.size()/2);

    ArrayList<E> left = new ArrayList<E>(0);
      for(E e : temp) left.add(e);

    temp = toSort.subList(toSort.size()/2, toSort.size());

    ArrayList<E> right = new ArrayList<E>(0);
      for(E e : temp) right.add(e);

    if(right.size() != 1) mergeSort(right);
    if(left.size() != 1) mergeSort(left);

    toSort = mergeSortedLists(left, right);
  }
}


public static <E extends Comparable<E>> List<E> mergeSortedLists(List<E> leftList, List<E> rightList)
{
  ArrayList<E> list = new ArrayList<E>();

  while(!leftList.isEmpty() && !rightList.isEmpty())
  {
    if((leftList.get(0)).compareTo(rightList.get(0)) <= 0)
      list.add(leftList.remove(0));

    else
      list.add(rightList.remove(0));
  }

  while(!leftList.isEmpty())
    list.add(leftList.remove(0));

  while(!rightList.isEmpty())
    list.add(rightList.remove(0));

  return list;
}

I normally have print statements for error checking, and those show that mergeSortedLists(…) sorts correctly and returns the correct list. Then, I assign the toSort variable in mergeSort(…) to whatever mergeSortedLists(…) returns. That assignment works. Now, it jumps back up a level to combine that list with a different list, and the changes seem to be lost. I have no clue what is happening.

  • 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-02T18:15:35+00:00Added an answer on June 2, 2026 at 6:15 pm

    Instead of this

    toSort = mergeSortedLists(left, right); 
    

    try

    toSort.clear();
    toSort.addAll(mergeSortedLists(left, right));
    

    The problem with your approach is that you reset the reference to the list, but that does not propagate back to the original function. The suggested version manipulates the original list that you were given the reference to. Since you don’t change the reference, the changes to the original list will show up at the caller after the function returns.

    To clarify: when you pass an argument to a function, a copy of that argument is created that functions as the local variable within the function. When you make changes to the value of that variable itself, those changes are made to the copy and not to the original (from which the copy was made when the function is called). In the suggested version, the copy is a reference, so altohugh the reference is copied, the object (here: list) is not, so the two rerefrences point to the same object. Thus changes made to the object via the copy (local variable), “show up” when the function returns.

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

Sidebar

Related Questions

I have a class that does some generic code for Writing to a Database.
I have a class that after it does some stuff, sends a JMS message.
I have a static util class that does some string manipulation on a bit
I have a Class that retrieves some data and images does some stuff to
The problem is this: I have an abstract class that does some work in
I have some code that does something like this: abstract class Data { Data(string
I have a constructor in a class that does some kind of logic: public
I have a class that does some time-consuming calculations. I'm trying to performance test
We have a class library that does some basic operations similar to an ORM,
I have a class that acts as a manager and does some work. A

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.