File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes binarysearch in arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "binarysearch in arrays" Watch "binarysearch in arrays" New topic
Author

binarysearch in arrays

Gajendra Kangokar
Ranch Hand

Joined: Dec 25, 2012
Posts: 72

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 Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

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 !


True person is moral, false is right!
Gajendra Kangokar
Ranch Hand

Joined: Dec 25, 2012
Posts: 72

yes got it,thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: binarysearch in arrays