Binary search non-recursive implementation
This commit is contained in:
parent
66c64c532c
commit
37b619725d
18
scala/15_bsearch/BSearch.scala
Normal file
18
scala/15_bsearch/BSearch.scala
Normal file
@ -0,0 +1,18 @@
|
||||
object BSearch {
|
||||
def search(nums: Array[Int], target: Int): Int = {
|
||||
var low = 0
|
||||
var high = nums.length - 1
|
||||
while(low <= high){
|
||||
val mid = low + ((high - low) >> 2)
|
||||
if(nums(mid) > target){
|
||||
high = mid - 1
|
||||
} else if (nums(mid) < target){
|
||||
low = mid + 1
|
||||
} else {
|
||||
return mid
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user