Is there a replacement in Scala for Java\'s int Arrays.binarySearch(Object[] array, object)
?
The problem is that Scala\'s Arrays are not covariant, so I
It not that hard to just write it in scala
object BSearch {
def interative[T](array: Array[T], value: T)(implicit arithmetic: Numeric[T]): Int = {
var left: Int = 0;
var right: Int = array.length - 1;
while (right > left) {
val mid = left + (right - left) / 2
val comp = arithmetic.compare(array(mid), value)
if (comp == 0)
return mid; //negative if test < value
else if (comp > 0) //list(mid) > value
right = mid - 1;
else if (comp < 0) //list(mid) < value
left = mid + 1;
}
-1;
}
BSearch.interative(array, value)