# Code for Binary Search Algorithm (Scala)

What does Binary Search Algorithm do? Given a sorted array of n elements, write a function to search for the index of a given element (target).

## Process of working of Binary Search Algorithm?

• Search for the array by dividing the array in half repeatedly.
• Initially consider the actual array and pick the element at the middle index
• Keep a lower index i.e. 0 and higher index i.e. length of array
• If it is equal to the target element then return the index
• Else if it is greater than the target element then consider only the left half of array. (lower index = 0, higher = middle – 1)
• Else if it is less than the target element then consider only the right half of array. (lower index = middle + 1, higher = length of array)
• Return -(insertion index + 1) if the target element is not found in the array (If the lower index is greater than or equal to higher index). Some simpler implementations just return -1 if the element is not found. The offset of 1 must be added as the insertion index might be 0 (the searched value might be smaller than all elements in the array). As indexing starts at 0, this must be distinguishable from the case where the target element has the index 0.

## Time Complexity of Binary Search Algorithm

• O(log n) Worst Case
• O(1) Best Case (If middle element of initial array is the target element)

## Space Complexity of Binary Search Algorithm

• O(1) For iterative approach
• O(1) For recursive approach if tail call optimization is used, O(log n) due to recursion call stack, otherwise

## Scala Code for Binary Search Algorithm

For copying this code to your local Code Editor please click little arrow on top right corner just next to “Binary Search Algorithm” and then hit Command + C if your on Mac or Control + C if your on Windows. This will copy the code to your laptop’s Clipboard and then you can go to your local Code Editor and paste it.

``````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
* @return
*   - index of the @elem otherwise -1
*/

def binarySearch(arr: List[Int], elem: Int, fromIndex: Int, toIndex: Int): Int = {

@tailrec
def SearchImpl(lo: Int, hi: Int): Int = {
if (lo > hi)
-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)
}

/** @param arr
*   - a sequence of integers
* @param elem
*   - a integer to search for in the @args
* @return
*/
def lowerBound(arr: List[Int], elem: Int): Int = {
lowerBound(arr, elem, 0, arr.length - 1)
}

/** @param arr
*   - a sequence of integers
* @param elem
*   - a integer to search for in the @args
* @param lo
*   - lowest value index
* @param hi
*   - highest value index
* @return
*/
def lowerBound(arr: List[Int], elem: Int, lo: Int, hi: Int): Int = {
if (lo == hi) lo
else {
val m: Int = lo + (hi - lo) / 2
arr(m) match {
case mv if (mv < elem)  => lowerBound(arr, elem, m + 1, hi)
case mv if (mv >= elem) => lowerBound(arr, elem, lo, m)
}
}
}
}``````