Hi I tried to implement a generic binary search algorithm in scala. Here it is :
type Ord ={
def <(x:Any):Boolean
def >(x:Any):Boolean
}
def binSearch[T <: Ord ](x:T,start:Int,end:Int,t:Array[T]):Boolean = {
if (start > end) return false
val pos = (start + end ) / 2
if(t(pos)==x) true
else if (t(pos) < x) binSearch(x,pos+1,end,t)
else binSearch(x,start,pos-1,t)
}
everything is OK until I tried to actually use it (xD) :
binSearch(3,0,4,Array(1,2,5,6))
the compiler is pretending that Int is not a member of Ord, but as I know class Int has <
and >
methods.
Well what shall I do to solve this weird problem?
Thanks