Results 1 to 4 of 4
  1. #1
    coldfire is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default Using Merge Sort to sort an ArrayList of Strings

    nevermind!
    Last edited by coldfire; 03-16-2009 at 01:02 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what are the purpose of parameters first, last and mid if an ArrayList has properties that can get you these values easily (and much more, judging by your code)? taking advantage of this, you can easily avoid a bad index and a lot of trouble. if you're using a class like this, you need to check out the api.

  3. #3
    coldfire is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    I found the problem: the newarray ArrayList was empty.

    But can anyone help me with how to do this:

    copy newarray to array[first to (last-1)]

    which should be the last line in the merge method, in place of "array = newarray"?

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you can get from the original arraylist and just add to the new arraylist. i don't know if there is a more efficient way to do this off the top of my head, but you should just read the arraylist api and figure out for yourself whatever solution you are most comfortable with.

    also, "array = newarray" will do nothing for your program as a whole. in recursion, you're passing a reference of your arraylist down, so by saying array = newarray, the parent call will not know of this reassignment. instead, you need to directly modify the "array".

Similar Threads

  1. Replies: 2
    Last Post: 02-02-2009, 12:29 PM
  2. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  3. Merge Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:43 PM
  4. Merge Sort Help
    By Hollywood in forum New To Java
    Replies: 5
    Last Post: 01-30-2008, 04:26 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 07:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •