File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes binarySearch() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "binarySearch() problem" Watch "binarySearch() problem" New topic
Author

binarySearch() problem

Ami Ambre
Ranch Hand

Joined: Dec 26, 2007
Posts: 58
unsuccessfully searches return int index that represent the insertion
point. The insertion point is the place in the collection where the element
would be inserted to keep the Collection properly sorted.Because positive
return value & 0 indicate successfully searches.
binarySearch() uses negative number to indicate insertion
point is -1.Therefore, the actual insertion point is (-(insertion
point)-1).If the insertion point search at element 2, the actual
insertion point return will be -3.

My question is unsuccessfully binarySearch() what it return.
index of the element or insertion point. what?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

Originally posted by Ami Ambre:
unsuccessfully searches return int index that represent the insertion
point. The insertion point is the place in the collection where the element
would be inserted to keep the Collection properly sorted.


Your answer !

Originally posted by Ami Ambre:

My question is unsuccessfully binarySearch() what it return.
index of the element or insertion point. what?



Your question !


Is that you want to clarify or I`m not getting it ?
[ September 17, 2008: Message edited by: Sagar Rohankar ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ranganath D
Greenhorn

Joined: Sep 16, 2008
Posts: 2
binarySearch() method returns the index of the search key if it is present in the list. otherwise, it returns (-(insertion point) - 1)

For eg: Your list/array is 1 3 5 8 9.

If you search for the element 5 it returns 2 which is the index of the key.
If you search for the element 4 it returns a negative number -3 (-2-1=-3). Because insertion index/point for the element 4 would be 2. So it returns -3.

Hope it clarifies your question.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: binarySearch() problem
 
Similar Threads
K & B book p. 659 - Explanation to self test question 16
Doubt in binarySearch
Confusion in understanding Comparator in Java
Wrong answer in Whizlabs Practice Exam 1, question 59
Question regarding binarySearch....