We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
The JDK offers binary search algorithms out of the box, for sorted arrays and lists.
The Mug BinarySearch class (it requires Guava dependency) is a generalization applicable to more use cases. For example:
double
Optional<Integer> index = BinarySearch.inSortedArrayWithTolerance(doubles, 0.0001).find(3.14)
Range<Integer> indexRange = BinarySearch.inSortedArrayWithTolerance(doubles, 0.0001).rangeOf(3.14)
long polynomial(int x) { return 5 * x * x * x + 3 * x + 2; } Optional<Integer> solvePolynomial(long y) { return BinarySearch.forInts() .find((low, x, high) -> Long.compare(y, polynomial(x)); }