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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:02:46+00:00 2026-06-17T07:02:46+00:00

I tried to implement a merge sort for a list using the added code.

  • 0

I tried to implement a merge sort for a list using the added code. The problem I encountered, which I have no idea what is causing it is: If I call recursiveMergeSort directly I get the correct sorted list back, but if I try to sort the list by calling mergeSort on it I only get a list of the first element.

I.E if in main I have a list = “d”,”a”,”f”,”g”,”b” then

list = recursiveMergeSort(list) // list will be "a","b","d","f","g"
mergeSort(list) // list will be "a"

What do I need to do for mergSort to give the same result as recursiveMergeSort?
(The public void function was given to me in the API, that’s why I created a private recursive function)

public static void mergeSort(List list) {
if (list == null) throw new NullPointerException("list");
    list = recursiveMergeSort(list);
    /* If I check list here, I get the correct result,
    only in the original it's wrong */
}
/** a recursive function for the merge sort  */
private static List recursiveMergeSort(List list) {

    int length = listLength(list);
    System.out.println(length);
    if (length <= 1) { // base case, list length 1
        return list;
    }

    // create sublists
    List left = new List(), right = new List();
    List.Node indexList = list.head;
    left.head = list.head;
    for (int i = 0; i < length / 2 - 1; i++ ) {
        indexList = indexList.getNext();
    }
    right.head = indexList.getNext();
    indexList.setNext(null);

    left = recursiveMergeSort(left); // recursively work on them
    right = recursiveMergeSort(right);
    List tempList = mergeLists(left, right); // final result
    return tempList;
}

(Merge lists just merges two ordered lists into an ordered list.)

  • 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-17T07:02:47+00:00Added an answer on June 17, 2026 at 7:02 am

    list is a reference variable, referencing a List value.

    You can’t change a referenced value just by changing the value of a variable that references it.

    Change your initial function to:

    public static List mergeSort(List list) {
    if (list == null) throw new NullPointerException("list");
        return recursiveMergeSort(list);
    }
    

    The value you return will be the sorted list.

    If you do need to change the argument in place, try:

    public static void mergeSort(List list) {
    if (list == null) throw new NullPointerException("list");
        List temp = recursiveMergeSort(list);
        list.clear();
        list.addAll(temp);
    }
    

    You can change an object by calling methods on it, or changing the values of fields in it, either the object itself or components of it. indexList in your recursiveMergeSort is another reference for nodes in your starting list, and you are altering it with the setNext(null)

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

Sidebar

Related Questions

Tried to implement a reader-writer problem using Blocking Queue with the code given here
I have a problem when trying to update foreign keys. Using the merge function
I tried to implement the following in less: nav > ul > li:first-child using:
I have tried to implement css sticky footer on my page but it doesn't
i have tried implement two methods recursive and dynamic method and both took 0
I tried to implement a simple Decorator Pattern in Java. The main idea is
I tried to implement a date selection using three ComboBox as shown below. contents
i tried to implement this code that move UIImageView's objects, i get a compiler
I am new to Java and have tried to implement mergesort in Java. However,
I have a requirement to create read-only merge fields in Word. I've already tried

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.