Binary Search is an algorithm that helps us find an element in a sorted array in O(log n) time. Its algorithm works on the principle of Divide and Conquer and it works only when the available data is sorted.
Now, when we are using binary search three situation arise:
- If the middle element is the element to be searched, we return the index of the middle element.
- If the middle element is smaller than the element to be searched, we search in the sub-array to the right (Mid to End) in the same way (Get the new middle and check all the three cases again). We search in the right sub-array because the data is sorted, thus all elements before middle element will also be either less than or equal to the middle element.
- If the middle element is greater than the element to be searched, we search in the sub-array to the left(Start to Mid).
This process is continued until either we find the element to be searched or the size of the sub-array reduces to zero.
Example:
There are three ways to perform Binary Search in Scala.
Recursive Approach
In recursive approach, we recursively call for the implemented binary search algorithm with updated values of start and end until either we match the middle element with the element to be searched or the array size reduces to zero. Below is the code for the recursive approach of Binary Search in Scala.
object GFG{
def RecursiveBinarySearch(arr : Array[Int],
Element _ to _ Search : Int)
(low : Int = 0 ,
high : Int = arr.length - 1 ) : Int =
{
if (low > high)
return - 1
var middle = low + (high - low) / 2
if (arr(middle) == Element _ to _ Search)
return middle
else if (arr(middle) > Element _ to _ Search)
return RecursiveBinarySearch(arr,
Element _ to _ Search)(low, middle - 1 )
else
return RecursiveBinarySearch(arr,
Element _ to _ Search)(middle + 1 , high)
}
def main(args : Array[String]){
var index = RecursiveBinarySearch(Array( 1 , 2 , 3 , 4 , 55 ,
65 , 75 ), 4 )( 0 , 6 );
if (index == - 1 )
print( "Not Found" )
else
print( "Element found at Index " + index)
}
}
|
Output
Element found at Index 3
Iterative approach
In iterative approach, we run a while loop until we either find the element to be searched or the array size reduces to zero. Below is the code for iterative approach of Binary Search in Scala.
object GFG{
def IterativeBinarySearch(arr : Array[Int],
Element _ to _ Search : Int) : Int =
{
var low = 0
var high = arr.length - 1
while (low <= high)
{
var middle = low + (high - low) / 2
if (arr(middle) == Element _ to _ Search)
return middle
else if (arr(middle) > Element _ to _ Search)
high = middle - 1
else
low = middle + 1
}
- 1
}
def main(args : Array[String])
{
var index = IterativeBinarySearch(Array( 1 , 2 , 3 , 4 , 55 ,
65 , 75 ), 65 );
if (index == - 1 )
print( "Not Found" )
else
print( "Element found at Index " + index)
}
}
|
<Output
Element found at Index 5
Pattern Matching And Functional Programming Approach
In this, we first match the middle element with the element to be searched. If the element is present, we return the index of it. Otherwise, we keep calling the created function with the updated parameters. Below is the code for the approach:
object GFG{
def FunctionalBinarySearch(arr : Array[Int],
Element _ to _ Search : Int) : Int =
{
def BinarySearch(arr : Array[Int],
Element _ to _ Search : Int,
low : Int, high : Int) : Int =
{
if (low > high)
return - 1
var middle = low + (high - low) / 2
arr match
{
case (arr : Array[Int]) if (arr(middle) ==
Element _ to _ Search) =>
middle
case (arr : Array[Int]) if (arr(middle) <
Element _ to _ Search) =>
BinarySearch(arr,
Element _ to _ Search,
middle + 1 , high)
case (arr : Array[Int]) if (arr(middle) >
Element _ to _ Search) =>
BinarySearch(arr,
Element _ to _ Search,
low, middle - 1 )
}
}
BinarySearch(arr, Element _ to _ Search, 0 , arr.length - 1 )
}
def main(args : Array[String]){
var index = FunctionalBinarySearch(Array( 1 , 2 , 3 , 4 , 55 ,
65 , 75 ), 44 );
if (index == - 1 )
print( "Element not found" )
else
print( "Element found at Index " + index)
}
}
|
Output
Element not found
Last Updated :
03 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...