Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me...

 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code.



Whats wrong with this?Thank you.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaturaka Gunatilaka wrote:Whats wrong with this?Thank you.

What makes you think there is something wrong with it ? TellTheDetails
 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Chaturaka Gunatilaka wrote:Whats wrong with this?Thank you.

What makes you think there is something wrong with it ? TellTheDetails


There is an out of bound exception.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaturaka Gunatilaka wrote:There is an out of bound exception.

Where ? TellTheDetails - read this link carefully before posting again.
 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error

Sorted values :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at sort.partition(sort.java:11)
at quickSort.main(quickSort.java:6)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please UseAMeaningfulSubjectLine instead of "Please help me" when you post a question.

ArrayIndexOutOfBoundsException means that you're trying to access an array using an invalid index. If the length of the array is N, then 0 upto and including N - 1 are valid indices.

Read the error message carefully. It tells you exactly in which line the error occurs. Carefully analyze your program to see what happens. Add some System.out.println(...) statements to print out useful information, to help you understand what's happening. You could also run the code using a debugger, so that you can step through the code line by line while it runs, which might help you understand what happens exactly.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more point... The partition(...) method should be used internally by method that actually implements the algorithm (if you really want Quicksort) and not invoked separately from main. So I would make that one private and change the implementation accordingly.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can always stick in System.out.println() statements to see what your code is doing. Whichever line is throwing the error, print out what index you are trying to reference. You will probably see you are trying to access index n on an array that has n elements. Since you can only use indexes 0 through n - 1, using n give an out of bounds exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic