Skip to content

Commit

Permalink
Binary search recursive implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchuz authored Oct 30, 2018
1 parent 37b6197 commit 44a2376
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions scala/15_bsearch/BSearchRecursive.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
}
}
}

0 comments on commit 44a2376

Please sign in to comment.