• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with using mergesort to sort a list of names alphabetically?  RSS feed

 
Bear Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to sort a list of names alphabetically, case-insensitive by using the mergesort technique.
I wrote this code and when I trace it through on paper with an example array of names, it should work, but when I run it with an actual txt file, it's not correctly alphabetical.
I'd appreciate it if someone could take a look at my code and give me some ideas on what my problem might be.
Thanks in advance! (note: I also posted this question to java-forums.org, as I've been working on this little problem for over five hours and am in desperate need of some help!)


 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear Kim,

Please TellTheDetails. What do you mean by it doesn't work? Is there any logical error or something else?

Please provide an example of actual output and expected output. Also provide the code you are using to read text from file.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to CodeRanch!
 
Bear Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The actual text file is very long, so I made a "test" file to test out my code.

I used a txt file that would make an array of {M, B, J, A, Z, C}

And it should come out as {A, B, C, J, M, Z}, but instead it prints out {A, C, Z, B, J, M}

Thanks for any help!
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I (still) haven't gone through your code, but here is what it seems:

You are splitting your array into left and right, and then recursively sorting it. After that, you are merging those.

Here, you've split {M, B, J, A, Z, C} and your output is {A, C, Z, B, J, M}. It seems to me that all you need is one more merge (I feel that way because from your output, left half of array is sorted, as well as right part).

Just check if number of merges you are doing are equal to ideal number of merges, and that if is there any logical failure in case of last merge.

I hope this helps.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println() is your best friend. put a bunch of them in all over your code. I often start with on at the beginning and end of each method...something like


I purposely don't indent them so I can take them out later. Run the code and see what it does. If something looks odd, put more println()'s in near where something strange is going on. sometimes, I even create throw-away methods that help me debug...like a printArray() method. I may then print the array that is being passed into the mergeSort() method each time, to see what it is REALLY getting.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!