This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General 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