开发者

How can I use binary search to search for the first item in a List greater than a specific value?

using Java. the List is sorted so I want to use binary se开发者_如何学运维arch. I'd prefer not to write extra code, is there any built-in method to do this?


Have you looked at Collections.binarySearch? That basically does the search part - you'd then need to look at whether the result was negative (i.e. an exact match wasn't found) and take the bitwise complement if so to find where the item would have been inserted.


Try JavaDocs for Arrays there are around 17 function definitions for binarySearcy()

Also here are a few implementations

EDIT:

Although you said you didn't want to write code, it's a sure fire way to get exactly what you want. And learn along the way :)

http://www.java-tips.org/java-se-tips/java.lang/binary-search-implementation-in-java.html http://en.wikibooks.org/wiki/Algorithm_Implementation/Search/Binary_search

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜