• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a merge sort for string arrays,  RSS feed

 
Alexander Fraher
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem printing out the descending order of my array. The array order goes like (Title,Studio,Year). I try to create to ints with the compareTo method but when the program is run the I get array out of bounds. Could the answer possibly be that in order not not have the out of bounds error, to create a for loop inside of the while?


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander Fraher wrote:I try to create to ints with the compareTo method but when the program is run the I get array out of bounds.

Could you show us the exact error message, and indicate which line it occurs at?

Could the answer possibly be that in order not not have the out of bounds error, to create a for loop inside of the while?

Unlikely. The ONLY reason you get ArrayIndexOutOfBoundsException for an array a is that the index value (in your case i, j or n) is < 0 or >= (note the '=') a.length; and the message should tell you what line (and therefore what array or arrays) it's happening on (my suspicion would be temp) AND the value of the invalid index.

Adding some println() statements to show the values of each index might also help you to see what's happening.

However, I'm not quite sure what you're trying to do in your merge() method. Maybe you could explain it to us, and in the process you might discover the bug for yourself. Adding some comments might also help, but do keep your lines short.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander Fraher wrote:I'm having a problem printing out the descending order of my array...

Another point: You could probably save yourself quite a lot of code and complexity in your sort by having your Movie2 class implement Comparable (java.lang.Comparable ←click). Then you would only have to run compareTo() on the array elements, rather than on each field.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander Fraher wrote:I'm having a problem printing out the descending order of my array...

Yet another point: You could also help yourself out by not having two array references on the same line. For example, if you wrote your while loop as follows:you would then know exactly which array was causing the problem when you get your error message.

I'd also check the condition for your while loop. What exactly do you want it to do?

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you start by implementing merge sort on an int[].
Then try things like merge sort on a String[] because String already implements Comparable<String>
Then implement Comparable<Movie> in your class, then later convert your merge sort method to sort your Movie[].
You will write things like arr[i] > arr[j] to sort ints. Later, when you have your array of Comparable objects, you can replace that with arr[i].compareTo(arr[j]) > 0 but the rest of the logic will remain unchanged.
 
Alexander Fraher
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Alexander Fraher wrote:I'm having a problem printing out the descending order of my array...

Yet another point: You could also help yourself out by not having two array references on the same line. For example, if you wrote your while loop as follows:you would then know exactly which array was causing the problem when you get your error message.

I'd also check the condition for your while loop. What exactly do you want it to do?

HIH

Winston


The hiMovie is causing the problem, The conditions for my while loop is to continue going until the the variable "i" is less or equal to mid or if the variable "j" is less than or equal to high. It is just in order to run through all parts of the array until the array is finished.
And the error I have is

and the desired output of this program is like this http://prntscr.com/35xlcb
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander Fraher wrote:The hiMovie is causing the problem, The conditions for my while loop is to continue going until the the variable "i" is less or equal to mid or if the variable "j" is less than or equal to high. It is just in order to run through all parts of the array until the array is finished.

Yes, but it also assumes that the difference between low and mid and mid+1 and high are the same, which, if high-low is odd, cannot possibly be true.

You therefore need to allow for the possibility of an "odd man out" scenario in your loop condition, or force the two differences to be the same.

Like I say, you could help yourself out by adding println() statements, so that you can see the index values for each iteration of the loop. That will help you to work out why things are going wrong. The value the error message is showing you is 10, which almost certainly means that your array a has only 10 elements (indexed from 0 to 9).

However, not knowing exactly what your merge() method is trying to do, it's difficult to advise further.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!