×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Binary Search in Scala

By IncludeHelp Last updated : November 16, 2024

Searching is one of the important concepts in programming that has found applications in advanced concepts. There are a lot of algorithms to search for an element from the array.

Here, we will learn about the Binary search algorithm in Scala.

Binary Search

It is a search algorithm to find an element in the sorted array. The time complexity of binary search is O(log n). The working principle of binary search is divide and conquer. And the array is required to be sorted array for searching the element.

How does Binary Search Work?

In binary search algorithm, we take the middle element of the array. And search the required element at this position. There are three cases that arise on comparing the middle element of the array with the searchElement.

  • Case 1: If (middle element == searchElement), return the index of the element.
  • Case 2: If (middle element < searchElement), we will search the element in the subarray starting with index after middle index value and ending at the last index of the array.
  • Case 3: If (middle element > searchElement), we will search the element in the subarray starting with the first index and ending at the index less than middle index.

The sorting algorithm will continue until the element found or the size of the sub-array created becomes 0.

Example

Example to show how binary search works:

Array = {1, 4, 9, 10, 16, 22, 26, 29, 32, 36}
searchElement = 4

size = 10

Iteration 1:
start = 0, end = 9,  
mid = (start + end)/2 = (0 + 9)/2 = 4.
Element at index 4 is 16. 
Element 16 > 4. Update end to (4-1) = 3.

Iteration 2:
start = 0, end = 3,  
mid = (start + end)/2 = (0 + 3)/2 = 1.
Element at index 1 is 4. 
Element at 4 == 4. Element found at index 1. 

Binary search can be implemented using recursive approach or iterative approach.

Binary Search Using Recursive Approach

In the recursive approach, we will create a function that will be called for each subarray.

Implementation

object BinarySearch { 

  def BinarySearchRec(arr: Array[Int], Element_to_Search: Int, start: Int, end: Int): Int = {
    if (start > end) 
      return -1

    val mid = start + (end - start) / 2

    if (arr(mid) == Element_to_Search) 
      return mid
    else if (arr(mid) > Element_to_Search) 
      return BinarySearchRec(arr, Element_to_Search, start, mid - 1)
    else
      return BinarySearchRec(arr, Element_to_Search, mid + 1, end)
  }

  def main(args: Array[String]): Unit = {
    val searchElement = 4
    val sortedArray = Array(1, 4, 9, 10, 16, 22, 26, 29, 32, 36)

    val elementIndex = BinarySearchRec(sortedArray, searchElement, 0, sortedArray.length - 1) 

    if (elementIndex == -1) 
      println("Not Found") 
    else
      println(s"Element found at index value $elementIndex") 
  } 
}

Output

Element found at index value 1

Here, we have created an array sortedArray and searchElement, the element to be searched. To find the index of the element, we will use the binary search and implementing it using the recursive approach. The function call itself with different subarrays, based on the searchElement's comparison value with the mid element of the current array.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.