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

  • Home
  • SEARCH
  • 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 4553754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:59:29+00:00 2026-05-21T16:59:29+00:00

For whatever reason, despite the fact that it’s entirely generic and works with integers

  • 0

For whatever reason, despite the fact that it’s entirely generic and works with integers whenever it’s tested with strings it seems to skip the last iteration of merging. I’ve scoured my code for a couple hours now and I can’t see why it isn’t working correctly so any insight anyone has would be well received!

import java.util.Iterator; import
java.util.LinkedList;

/** * * @author paul */ public
class MergeSort> {

LinkedList<T> theList;

MergeSort(LinkedList<T> toBeSorted) {
    theList = toBeSorted;
}

public LinkedList<T> sort() {
    return trueSort(theList);
}

private LinkedList<T> trueSort(LinkedList<T> sorting) {
    if (sorting.size() <= 1) {
        return sorting;
    }
    LinkedList<T> left, right, sorted;
    left = new LinkedList<T>();
    right = new LinkedList<T>();
    int middle = sorting.size() / 2;
    Iterator<T> sojourner = sorting.iterator();
    for (int i = 0; sojourner.hasNext(); i++) {
        if (i < middle) {
            left.add(sojourner.next());
        } else {
            right.add(sojourner.next());
        }
    }
    return trueMerge(trueSort(left),

trueSort(right));
}

private LinkedList<T> trueMerge(LinkedList<T> left,

LinkedList right) {
LinkedList result = new LinkedList();
while (left.size() > 0 || right.size() > 0) {
if (left.size() > 0 && right.size() > 0) {
if (left.getFirst().compareTo(right.getFirst())
< 0) {
result.add(left.pop());
} else {
result.add(right.pop());
}
} else if (left.size() > 0) {
result.add(left.pop());
} else {
result.add(right.pop());
}
}
return result;
} }

Here’s my main java file

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

/**
*
* @author paul
*/
public class Main {

    public static Random Rand;

    public static int randomNumber(int min, int max) {
  return min + (int) (Rand.nextDouble() * ((max - min) +

1));
}

    public static <T> String getString(LinkedList<T> linkInt) {
  String s = "";
  Iterator<T> interLink = linkInt.iterator();
  for (int i = 0; interLink.hasNext(); i++) {
    s = s + interLink.next().toString();
    if (interLink.hasNext()) {
    s = s + ", ";
    }
    if ((i + 1) % 10 == 0) {
    s = s + "\n";
    }
  }
  return s;
    }

    /**
  * @param args the command line arguments
  */
    public static void main(String[] args) {
  Rand = new Random();

  LinkedList<Integer> numbers = new LinkedList<Integer>();
  for(int i = 0; i< 100;i++){
    numbers.add(randomNumber(1,1000));
  }
  System.out.println(getString(numbers));
  MergeSort m = new MergeSort(numbers); //change this

later to be purely static
numbers = m.sort();
System.out.println(getString(numbers));

  LinkedList<String> words = new LinkedList<String>();
  words.add("Hello");
  words.add("MY");
  words.add("name");
  words.add("Is");
  words.add("Barthoal");
  words.add("I");
  words.add("Enjoy");
  words.add("long");
  words.add("beach");
  words.add("walks");
  words.add("would");
  words.add("you");
  words.add("like");
  words.add("to");
  words.add("come");
  words.add("Join");
  words.add("me");
  words.add("in");
  words.add("my");
  words.add("StarDestroyer-MobileHome?

(TM)”);
System.out.println(getString(words));
MergeSort mm = new MergeSort(words);
words = mm.sort();
System.out.println(getString(words));
}
}

This outputs:

304, 842, 342, 794, 574, 99, 250, 885,
408, 387, 899, 73, 391, 883, 771,
848, 968, 504, 129, 370, 994, 897,
649, 345, 983, 326, 688, 547, 541,
567, 777, 987, 201, 326, 298, 959,
166, 962, 864, 797, 512, 505, 609,
208, 21, 43, 458, 442, 138, 570, 455,
442, 516, 294, 406, 310, 215, 212,
397, 98, 938, 496, 263, 973, 571,
861, 687, 276, 927, 608, 421, 831,
820, 510, 68, 172, 504, 8, 976, 992,
68, 497, 33, 233, 607, 587, 611, 695,
834, 338, 448, 978, 359, 413, 1, 819,
18, 977, 693, 649

1, 8, 18, 21, 33, 43, 68, 68, 73, 98,
99, 129, 138, 166, 172, 201, 208, 212,
215, 233, 250, 263, 276, 294, 298,
304, 310, 326, 326, 338, 342, 345,
359, 370, 387, 391, 397, 406, 408,
413, 421, 442, 442, 448, 455, 458,
496, 497, 504, 504, 505, 510, 512,
516, 541, 547, 567, 570, 571, 574,
587, 607, 608, 609, 611, 649, 649,
687, 688, 693, 695, 771, 777, 794,
797, 819, 820, 831, 834, 842, 848,
861, 864, 883, 885, 897, 899, 927,
938, 959, 962, 968, 973, 976, 977,
978, 983, 987, 992, 994

Hello, MY, name, Is, Barthoal, I,
Enjoy, long, beach, walks, would,
you, like, to, come, Join, me, in, my,
StarDestroyer-MobileHome? (TM)

Barthoal, Enjoy, Hello, I, Is, Join,
MY, StarDestroyer-MobileHome? (TM),
beach, come, in, like, long, me, my,
name, to, walks, would, you

As you can see, the numbers are fully sorted. The strings seem to have missed the last iteration of merging. So what exactly went 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-21T16:59:29+00:00Added an answer on May 21, 2026 at 4:59 pm

    String comparison is case sensitive. That is, all uppercase strings will be placed before the lower case ones. Your merge sort algorithm should allow to take an own comparator which could be String.CASE_INSENSITIVE_ORDER for your use case.

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

Sidebar

Related Questions

I have an ASP.NET web application that, for whatever reason, when it is deployed
For whatever reason, our company has a coding guideline that states: Each class shall
I have a linq query that for whatever reason is not coming back ordered
For whatever reason ths works when run from console: var wh=window.innerHeight; var h= wh-345;
For whatever reason, lowercase 'o's are invisible in my textarea. Every other letter works
I have an existing database that was setup (for whatever reason??) to store a
For whatever reason - my update panel is not updating. Now please note that
Our style guide says (for whatever reason that isn't important to this question) no
I'm building a python library that implements a task queue. If for whatever reason
if a WCF service goes down for whatever reason, is it possible that another

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.