-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbinarySearch.scala
52 lines (41 loc) · 1.11 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
// Scala code for Recursive Binary Search
// Creating object
object GFG{
// Creating a recursive Binary Search function
def RecursiveBinarySearch(arr: Array[Int],
Element_to_Search: Int)
(low: Int = 0,
high: Int = arr.length - 1): Int =
{
// If element not found
if (low > high)
return -1
// Getting the middle element
var middle = low + (high - low) / 2
// If element found
if (arr(middle) == Element_to_Search)
return middle
// Searching in the left half
else if (arr(middle) > Element_to_Search)
return RecursiveBinarySearch(arr,
Element_to_Search)(low, middle - 1)
// Searching in the right half
else
return RecursiveBinarySearch(arr,
Element_to_Search)(middle + 1, high)
}
// Creating main function
def main(args: Array[String]){
// Calling the binary search function and
// storing its result in index variable
var index = RecursiveBinarySearch(Array(1, 2, 3, 4, 55,
65, 75), 4)(0, 6);
// If value not found
if(index == -1)
print("Not Found")
// Else print the index where
// the value is found
else
print("Element found at Index " + index)
}
}