-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathBinarySearch.scala
57 lines (48 loc) · 1.57 KB
/
BinarySearch.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package Search
/**
* An implementation of binary search algorithm to search an element in a sorted list
*/
import scala.annotation.tailrec
object BinarySearch {
/**
* @param arr - a sequence of integers
* @param elem - a integer to search for in the @args
* @return - index of the @elem otherwise -1
*/
def binarySearch(arr: List[Int], elem: Int): Int = {
binarySearch(arr, elem, 0, arr.length)
}
/**
* @param arr - a sequence of integers
* @param elem - a integer to search for in the @args
* @param fromIndex - the index of the first element (inclusive) to be searched
* @param toIndex - toIndex the index of the last element (exclusive) to be searched
* @param returnInsertIdx - if `true`, returns info about the insertion index
* @return - index of the @elem otherwise -1. If `returnInsertIdx` is true, returns
* (-insertion_idx - 1) where insertion_idx is the 1st index where `elem` can be
* inserted into `arr` and `arr` is still sorted.
*/
def binarySearch(
arr: List[Int],
elem: Int,
fromIndex: Int,
toIndex: Int,
returnInsertIdx: Boolean = false
): Int = {
@tailrec
def SearchImpl(lo: Int, hi: Int): Int = {
if (lo > hi) {
if(returnInsertIdx) -lo - 1 else -1
}
else {
val mid: Int = lo + (hi - lo) / 2
arr(mid) match {
case mv if (mv == elem) => mid
case mv if (mv <= elem) => SearchImpl(mid + 1, hi)
case _ => SearchImpl(lo, mid - 1)
}
}
}
SearchImpl(fromIndex, toIndex-1)
}
}