Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object BusquedaBinaria extends App {
- val arra3 : Array[Int] = new Array(10) // Utilizo el array del ejercicio 1c para probar mi función
- for(i <- 0 until arra3.length)
- arra3(i) = 7*i
- val j = búsquedaBinariaRec(14,arra3)
- println(j)
- def búsquedaBinariaRec[A](x : A, xs : Array[A])(implicit ord : Ordering[A]) : Option[Int] = {
- import ord._
- def buscar(x : A, xs : Array[A], izq : Int, der : Int) : Option[Int] = {
- return if (izq+der/2 < 0 || izq+der/2 > xs.length-1)
- None
- else if (x == xs(izq+der/2))
- Some(izq+der/2)
- else if (x<xs(izq+der/2))
- buscar(x,xs,izq,izq+der/2-1)
- else
- buscar(x,xs,izq+der/2+1,der)
- }
- return buscar(x,xs,0,xs.length-1) //inicialmente, queda todo por buscar
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement