Hello everyone, I just started using Scala and I have a problem that I can't get out of, I need to find an element within an array by recursion with an auxiliary function called search, which takes parameters from the left and right ends. The truth is that I am very overwhelmed because the compiler does not give me any error, it just seems that it does not enter the BinaryRec search method when I put it in the println. First of all, thanks for your help, this is what I have so far:
import scala.io.StdIn._
def busquedaBinariaRec[A](x:A,xs:Array[A])(implicit ord : Ordering[A]): Any ={
//implementarlos recursivamente sin usar bucles
import ord._
buscar(0, xs.length -1)
def buscar (izq:Int, der:Int):Any = {
var indice : Option[Int] = None
var izq = 0
var der = xs.length -1
if (indice.isEmpty && izq <= der) {
val cen = (izq+der)/2
val y = xs(cen)
if (x==y) indice=Some(cen)
else if (x>y)
izq = cen +1
else
der = cen-1
}
return indice
}
}
println("¿Cual es el Array?")
val xs = Array[Int](1, 5, 6, 10, 14, 20, 50) //El Array sobre el que queramos realizar la busqueda lo cambiamos aqui
println(xs.mkString("Array(", ", ", ")"))
println("Introduce el elemento que quieras buscar")
val n: Int = readInt()
println(busquedaBinariaRec(n, xs))
}