This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

binarysearch in arrays

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys

the output of the above code is

one two three four
one= 0
two= 1
three= -2
four= -1

My question, why is the output -2 and -1 for binarysearch for three and four?
Thank you
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

binarySearch requires your array to be sorted in ascending order using compareTo method of array's elements
or sorted by using your own Comparator.
Only in such case binarySearch will work correctly !
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes got it,thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic