algo/scala/15_bsearch/BSearchRecursive.scala
2018-10-30 16:27:06 +08:00

21 lines
648 B
Scala

object BSearchRecursive {
def search(nums: Array[Int], target: Int): Int = {
return searchInternal(nums, target, 0, nums.length - 1)
}
def searchInternal(nums:Array[Int], target: Int, low: Int, high: Int): Int = {
if(low <= high){
val mid = low + ((high - low) >> 2)
if(nums(mid) > target){
searchInternal(nums, target, low, mid - 1)
} else if (nums(mid) < target){
searchInternal(nums, target, mid + 1, high)
} else {
return mid
}
}else{
return -1
}
}
}