• Post Reply Bookmark Topic Watch Topic
  • New Topic

Merge Sort Problem  RSS feed

 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a merge sort program. which looks lilke



It compiles fine but while running it I get a ArrayIndexOutOfBoundsException Exception like:


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at MergeSort.subArray(MergeSort.java:61)
at MergeSort.mergeSort(MergeSort.java:19)
at MergeSort.mergeSort(MergeSort.java:17)
at MergeSort.main(MergeSort.java:5)


I found out the cause mostly. While in the second recursion of "mergeSort" the length of "data" array becomes half and I dont see any other pointers or refrences pointing to this array and changing its data.
I tried keeping several Sys.out's but not getting the cause.
Can anyone please help?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of 'end' when you get the NPE? what is tempArray.length? Waht is dataArray.length?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:It compiles fine but while running it I get a Nullpointer Exception like:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at MergeSort.subArray(MergeSort.java:61)

You say you are getting NPE whereas the stack trace shows ArrayIndexOutOfBoundsException.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Amit- It was a mistake typing the wrong exception.. Please Ignore that.

@Fred- dataArray is an array containing elemets to sort using merge sort technique.
tempArray is a part main array "dataArray".
and value of "end" is 2 when I get Exception.
do you see some problem in that??
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Fred- dataArray is an array containing elemets to sort using merge sort technique.
tempArray is a part main array "dataArray".
and value of "end" is 2 when I get Exception.
do you see some problem in that??

And ".length" is kept for debugging purpose nothing much.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:And ".length" is kept for debugging purpose nothing much.

no...

Each array has a .length attribute. an ArrayOutOfBounds exception means that your array is (say) seven elements long, and you are trying to access the eighth element.

So, just before line 61, print out what i is, what j is, what tempArray.length is, and what dataArray.length is. You'll see that either i or j is trying to access an element beyond the end of tempArray or dataArray.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: So, just before line 61, print out what i is, what j is, what tempArray.length is, and what dataArray.length is. You'll see that either i or j is trying to access an element beyond the end of tempArray or dataArray.


I did what exactly you said :



Output for that is like


Start = 0 End = 2
i--->>>0 j--->>0
dataArray.length in loop--6
tempArray.length in loop--3
i--->>>1 j--->>1
dataArray.length in loop--6
tempArray.length in loop--3
i--->>>2 j--->>2
dataArray.length in loop--6
tempArray.length in loop--3
Start = 0 End = 0
i--->>>0 j--->>0
dataArray.length in loop--3
tempArray.length in loop--1
Start = 1 End = 2
i--->>>1 j--->>0
dataArray.length in loop--3
tempArray.length in loop--1
i--->>>2 j--->>1
dataArray.length in loop--3
tempArray.length in loop--1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at MergeSort.subArray(MergeSort.java:63)
at MergeSort.mergeSort(MergeSort.java:19)
at MergeSort.mergeSort(MergeSort.java:17)
at MergeSort.main(MergeSort.java:5)


The problem is after one recursion the main array's length becomes half. I dont find the reason for that.
Is there any thing more that i have look for?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!