binary search


See On Github

Data

Source Code

/* SCALA Implementation of Binary Search.

  Though the corner cases are covered. But still if you find any additions to it,
  please do add a test for it.

  Any improvements/tests in the code is highly appreciated.
 */

class BinarySearch {

  def binarySearch(targetArray: Array[Int], numberToBeSearched: Int): Int = {

    def binarySearchSupport(list: Array[Int], target: Int, start: Int, end: Int): Int = {
      if (start>end) return -1

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

      list match {
        case (arr:Array[Int]) if (arr(mid)==target) => mid
        case (arr:Array[Int]) if (arr(mid)>target) => binarySearchSupport(list, target, start, mid-1)
        case (arr:Array[Int]) if (arr(mid)<target) => binarySearchSupport(list, target, mid+1, end)
      }
    }
    binarySearchSupport(targetArray, numberToBeSearched, 0, targetArray.length-1)
  }
}
import org.scalatest.{Matchers, ShouldMatchers, FunSuite}

class BinarySearchTest extends FunSuite with Matchers {

  test("Should search for") {

    val objectForBinarySearch = new BinarySearch

    objectForBinarySearch.binarySearch(Array(1,2,3,4,5),3) should be(2)
    objectForBinarySearch.binarySearch(Array(1,2,3,4,5),4) should be(3)
    objectForBinarySearch.binarySearch(Array(0,0,0,1,1,1),1) should be(3)
  }
}