Win a copy of Functional Reactive Programming this week in the Other Languages 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: 512
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