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
精彩评论